Skip to content

Commit fa0cf56

Browse files
committed
Start renaming fields so it's clearer these aren't just arguments
1 parent bff684c commit fa0cf56

File tree

3 files changed

+83
-102
lines changed

3 files changed

+83
-102
lines changed

src/attr/_make.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,23 @@ class ClassProps(NamedTuple):
117117
"""
118118
Effective class properties as derived from parameters to attr.s() or
119119
define() decorators.
120+
121+
.. versionadded:: 25.4.0
120122
"""
121123

122-
exception: bool
123-
slots: bool
124-
frozen: bool
124+
is_exception: bool
125+
is_slotted: bool
126+
is_frozen: bool
127+
is_kw_only: bool
128+
force_kw_only: bool
125129
init: bool
126130
repr: bool
127131
eq: bool
128132
order: bool
129133
hash: _Hashability
130134
match_args: bool
131-
kw_only: bool
132-
force_kw_only: bool
133-
weakref_slot: bool
134-
auto_attribs: bool
135+
has_weakref_slot: bool
135136
collect_by_mro: bool
136-
auto_detect: bool
137-
auto_exc: bool
138137
cache_hash: bool
139138
str: bool
140139
getstate_setstate: bool
@@ -702,28 +701,29 @@ def __init__(
702701
self,
703702
cls: type,
704703
these,
705-
attrs_params: ClassProps,
704+
auto_attribs: bool,
705+
props: ClassProps,
706706
):
707707
attrs, base_attrs, base_map = _transform_attrs(
708708
cls,
709709
these,
710-
attrs_params.auto_attribs,
711-
attrs_params.kw_only,
712-
attrs_params.force_kw_only,
713-
attrs_params.collect_by_mro,
714-
attrs_params.field_transformer,
710+
auto_attribs,
711+
props.is_kw_only,
712+
props.force_kw_only,
713+
props.collect_by_mro,
714+
props.field_transformer,
715715
)
716716

717717
self._cls = cls
718-
self._cls_dict = dict(cls.__dict__) if attrs_params.slots else {}
718+
self._cls_dict = dict(cls.__dict__) if props.is_slotted else {}
719719
self._attrs = attrs
720720
self._base_names = {a.name for a in base_attrs}
721721
self._base_attr_map = base_map
722722
self._attr_names = tuple(a.name for a in attrs)
723-
self._slots = attrs_params.slots
724-
self._frozen = attrs_params.frozen
725-
self._weakref_slot = attrs_params.weakref_slot
726-
self._cache_hash = attrs_params.cache_hash
723+
self._slots = props.is_slotted
724+
self._frozen = props.is_frozen
725+
self._weakref_slot = props.has_weakref_slot
726+
self._cache_hash = props.cache_hash
727727
self._has_pre_init = bool(getattr(cls, "__attrs_pre_init__", False))
728728
self._pre_init_has_args = False
729729
if self._has_pre_init:
@@ -734,16 +734,16 @@ def __init__(
734734
self._pre_init_has_args = len(pre_init_signature.parameters) > 1
735735
self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False))
736736
self._delete_attribs = not bool(these)
737-
self._is_exc = attrs_params.exception
738-
self._on_setattr = attrs_params.on_setattr
737+
self._is_exc = props.is_exception
738+
self._on_setattr = props.on_setattr
739739

740-
self._has_custom_setattr = attrs_params.has_custom_setattr
740+
self._has_custom_setattr = props.has_custom_setattr
741741
self._wrote_own_setattr = False
742742

743743
self._cls_dict["__attrs_attrs__"] = self._attrs
744-
self._cls_dict["__attrs_props__"] = attrs_params
744+
self._cls_dict["__attrs_props__"] = props
745745

746-
if attrs_params.frozen:
746+
if props.is_frozen:
747747
self._cls_dict["__setattr__"] = _frozen_setattrs
748748
self._cls_dict["__delattr__"] = _frozen_delattrs
749749

@@ -775,7 +775,7 @@ def __init__(
775775
# no on_setattr.
776776
self._on_setattr = None
777777

778-
if attrs_params.getstate_setstate:
778+
if props.getstate_setstate:
779779
(
780780
self._cls_dict["__getstate__"],
781781
self._cls_dict["__setstate__"],
@@ -1503,10 +1503,11 @@ def wrap(cls):
15031503
msg = "Invalid value for cache_hash. To use hash caching, hashing must be either explicitly or implicitly enabled."
15041504
raise TypeError(msg)
15051505

1506-
attrs_params = ClassProps(
1507-
exception=is_exc,
1508-
frozen=is_frozen,
1509-
slots=slots,
1506+
props = ClassProps(
1507+
is_exception=is_exc,
1508+
is_frozen=is_frozen,
1509+
is_slotted=slots,
1510+
collect_by_mro=collect_by_mro,
15101511
init=_determine_whether_to_implement(
15111512
cls, init, auto_detect, ("__init__",)
15121513
),
@@ -1523,13 +1524,9 @@ def wrap(cls):
15231524
),
15241525
hash=hashability,
15251526
match_args=match_args,
1526-
kw_only=kw_only,
1527+
is_kw_only=kw_only,
15271528
force_kw_only=force_kw_only,
1528-
weakref_slot=weakref_slot,
1529-
auto_attribs=auto_attribs if auto_attribs is not None else False,
1530-
collect_by_mro=collect_by_mro,
1531-
auto_detect=auto_detect,
1532-
auto_exc=is_exc,
1529+
has_weakref_slot=weakref_slot,
15331530
cache_hash=cache_hash,
15341531
str=str,
15351532
getstate_setstate=_determine_whether_to_implement(
@@ -1544,28 +1541,30 @@ def wrap(cls):
15441541
field_transformer=field_transformer,
15451542
)
15461543

1547-
builder = _ClassBuilder(cls, these, attrs_params)
1544+
builder = _ClassBuilder(
1545+
cls, these, auto_attribs=auto_attribs, props=props
1546+
)
15481547

1549-
if attrs_params.repr is True:
1548+
if props.repr is True:
15501549
builder.add_repr(repr_ns)
15511550

1552-
if attrs_params.str is True:
1551+
if props.str is True:
15531552
builder.add_str()
15541553

1555-
if attrs_params.eq is True:
1554+
if props.eq is True:
15561555
builder.add_eq()
1557-
if attrs_params.order is True:
1556+
if props.order is True:
15581557
builder.add_order()
15591558

15601559
if not frozen:
15611560
builder.add_setattr()
15621561

1563-
if attrs_params.hash is _Hashability.HASHABLE:
1562+
if props.hash is _Hashability.HASHABLE:
15641563
builder.add_hash()
1565-
elif attrs_params.hash is _Hashability.UNHASHABLE:
1564+
elif props.hash is _Hashability.UNHASHABLE:
15661565
builder.make_unhashable()
15671566

1568-
if attrs_params.init:
1567+
if props.init:
15691568
builder.add_init()
15701569
else:
15711570
builder.add_attrs_init()

tests/test_make.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -533,22 +533,19 @@ class C:
533533

534534
assert (
535535
ClassProps(
536-
exception=False,
537-
slots=True,
538-
frozen=True,
536+
is_exception=False,
537+
is_slotted=True,
538+
is_frozen=True,
539539
init=True,
540540
repr=True,
541541
eq=True,
542542
order=True,
543543
hash=_Hashability.HASHABLE,
544544
match_args=False,
545-
kw_only=True,
545+
is_kw_only=True,
546546
force_kw_only=True,
547-
weakref_slot=True,
548-
auto_attribs=True,
547+
has_weakref_slot=True,
549548
collect_by_mro=False,
550-
auto_detect=False,
551-
auto_exc=False,
552549
cache_hash=True,
553550
str=True,
554551
getstate_setstate=True,
@@ -571,22 +568,19 @@ class CDef:
571568

572569
assert (
573570
ClassProps(
574-
exception=False,
575-
slots=False,
576-
frozen=False,
571+
is_exception=False,
572+
is_slotted=False,
573+
is_frozen=False,
577574
init=True,
578575
repr=True,
579576
eq=True,
580577
order=True,
581578
hash=_Hashability.UNHASHABLE,
582579
match_args=True,
583-
kw_only=False,
580+
is_kw_only=False,
584581
force_kw_only=True,
585-
weakref_slot=True,
586-
auto_attribs=False,
582+
has_weakref_slot=True,
587583
collect_by_mro=False,
588-
auto_detect=False,
589-
auto_exc=False,
590584
cache_hash=False,
591585
str=False,
592586
getstate_setstate=False,
@@ -2019,23 +2013,21 @@ class C:
20192013
b = _ClassBuilder(
20202014
C,
20212015
None,
2016+
True,
20222017
ClassProps(
2023-
exception=False,
2024-
slots=True,
2025-
frozen=True,
2018+
is_exception=False,
2019+
is_slotted=True,
2020+
is_frozen=True,
20262021
init=True,
20272022
repr=True,
20282023
eq=True,
20292024
order=False,
20302025
hash=False,
20312026
match_args=True,
2032-
kw_only=False,
2027+
is_kw_only=False,
20332028
force_kw_only=False,
2034-
weakref_slot=False,
2035-
auto_attribs=False,
2029+
has_weakref_slot=False,
20362030
collect_by_mro=True,
2037-
auto_detect=False,
2038-
auto_exc=False,
20392031
cache_hash=False,
20402032
str=False,
20412033
getstate_setstate=True,
@@ -2058,23 +2050,21 @@ class C:
20582050
b = _ClassBuilder(
20592051
C,
20602052
None,
2053+
False,
20612054
ClassProps(
2062-
exception=False,
2063-
slots=True,
2064-
frozen=True,
2055+
is_exception=False,
2056+
is_slotted=True,
2057+
is_frozen=True,
20652058
init=True,
20662059
repr=True,
20672060
eq=True,
20682061
order=False,
20692062
hash=False,
20702063
match_args=True,
2071-
kw_only=False,
2064+
is_kw_only=False,
20722065
force_kw_only=False,
2073-
weakref_slot=False,
2074-
auto_attribs=False,
2066+
has_weakref_slot=False,
20752067
collect_by_mro=True,
2076-
auto_detect=False,
2077-
auto_exc=False,
20782068
cache_hash=False,
20792069
str=False,
20802070
getstate_setstate=True,
@@ -2163,23 +2153,21 @@ def our_hasattr(obj, name, /) -> bool:
21632153
b = _ClassBuilder(
21642154
C,
21652155
these=None,
2166-
attrs_params=ClassProps(
2167-
exception=False,
2168-
slots=False,
2169-
frozen=False,
2156+
auto_attribs=False,
2157+
props=ClassProps(
2158+
is_exception=False,
2159+
is_slotted=False,
2160+
is_frozen=False,
21702161
init=True,
21712162
repr=True,
21722163
eq=True,
21732164
order=False,
21742165
hash=False,
21752166
match_args=True,
2176-
kw_only=False,
2167+
is_kw_only=False,
21772168
force_kw_only=False,
2178-
weakref_slot=True,
2179-
auto_attribs=False,
2169+
has_weakref_slot=True,
21802170
collect_by_mro=True,
2181-
auto_detect=False,
2182-
auto_exc=False,
21832171
cache_hash=False,
21842172
str=False,
21852173
getstate_setstate=True,

tests/test_next_gen.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -477,22 +477,19 @@ class C:
477477

478478
assert (
479479
ClassProps(
480-
exception=False,
481-
slots=False,
482-
frozen=True,
480+
is_exception=False,
481+
is_slotted=False,
482+
is_frozen=True,
483+
is_kw_only=True,
484+
force_kw_only=False,
483485
init=True,
484486
repr=True,
485487
eq=True,
486488
order=True,
487489
hash=_Hashability.HASHABLE,
488490
match_args=False,
489-
kw_only=True,
490-
force_kw_only=False,
491-
weakref_slot=True,
492-
auto_attribs=True,
491+
has_weakref_slot=True,
493492
collect_by_mro=True,
494-
auto_detect=True,
495-
auto_exc=False,
496493
cache_hash=True,
497494
str=True,
498495
getstate_setstate=False, # because slots=False
@@ -514,22 +511,19 @@ class C:
514511

515512
assert (
516513
ClassProps(
517-
exception=False,
518-
slots=True,
519-
frozen=True,
514+
is_exception=False,
515+
is_slotted=True,
516+
is_frozen=True,
520517
init=True,
521518
repr=True,
522519
eq=True,
523520
order=False,
524521
hash=_Hashability.HASHABLE, # b/c frozen
525522
match_args=True,
526-
kw_only=False,
523+
is_kw_only=False,
527524
force_kw_only=False,
528-
weakref_slot=True,
529-
auto_attribs=True,
525+
has_weakref_slot=True,
530526
collect_by_mro=True,
531-
auto_detect=True,
532-
auto_exc=False,
533527
cache_hash=False,
534528
str=False,
535529
getstate_setstate=True,

0 commit comments

Comments
 (0)