Skip to content

Commit 9486ced

Browse files
committed
Improve field names
To make it clearer these are derived values and nothing you can pass into define/attr.s
1 parent 518bf6b commit 9486ced

5 files changed

Lines changed: 142 additions & 140 deletions

File tree

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Helpers
200200
... class CInspect:
201201
... pass
202202
>>> attrs.inspect(CInspect) # doctest: +ELLIPSIS
203-
ClassProps(is_exception=False, is_slotted=True, has_weakref_slot=True, is_frozen=False, kw_only=<KeywordOnly.NO: 'no'>, collect_by_mro=True, init=True, repr=True, eq=True, order=False, hash=<Hashability.UNHASHABLE: 'unhashable'>, match_args=True, str=False, getstate_setstate=True, on_setattr=<function pipe.<locals>.wrapped_pipe at ...>, field_transformer=None)
203+
ClassProps(is_exception=False, is_slotted=True, has_weakref_slot=True, is_frozen=False, kw_only=<KeywordOnly.NO: 'no'>, collect_by_mro=True, has_init=True, has_repr=True, has_eq=True, is_orderable=False, hashability=<Hashability.UNHASHABLE: 'unhashable'>, can_match=True, has_str=False, is_pickleable=True, on_setattr_hook=<function pipe.<locals>.wrapped_pipe at ...>, field_transformer=None)
204204

205205
.. autoclass:: attrs.ClassProps
206206
.. autoclass:: attrs.ClassProps.Hashability

src/attr/__init__.pyi

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,16 @@ class ClassProps:
157157
# kw_only: ClassProps.KeywordOnly
158158
kw_only: Any
159159
collect_by_mro: bool
160-
init: bool
161-
repr: bool
162-
eq: bool
163-
order: bool
164-
# hash: ClassProps.Hashability
165-
hash: Any
166-
match_args: bool
167-
str: bool
168-
getstate_setstate: bool
169-
on_setattr: _OnSetAttrType | None
160+
has_init: bool
161+
has_repr: bool
162+
has_eq: bool
163+
is_orderable: bool
164+
# hashability: ClassProps.Hashability
165+
hashability: Any
166+
can_match: bool
167+
has_str: bool
168+
is_pickleable: bool
169+
on_setattr_hook: _OnSetAttrType | None
170170
field_transformer: Callable[[Attribute[Any]], Attribute[Any]] | None
171171

172172
def __init__(
@@ -178,16 +178,16 @@ class ClassProps:
178178
# kw_only: ClassProps.KeywordOnly
179179
kw_only: Any,
180180
collect_by_mro: bool,
181-
init: bool,
182-
repr: bool,
183-
eq: bool,
184-
order: bool,
185-
# hash: ClassProps.Hashability
186-
hash: Any,
187-
match_args: bool,
188-
str: bool,
189-
getstate_setstate: bool,
190-
on_setattr: _OnSetAttrType,
181+
has_init: bool,
182+
has_repr: bool,
183+
has_eq: bool,
184+
is_orderable: bool,
185+
# hashability: ClassProps.Hashability
186+
hashability: Any,
187+
can_match: bool,
188+
has_str: bool,
189+
is_pickleable: bool,
190+
on_setattr_hook: _OnSetAttrType,
191191
field_transformer: Callable[[Attribute[Any]], Attribute[Any]],
192192
) -> None: ...
193193
@property

src/attr/_make.py

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,9 @@ def __init__(
688688
self._slots = props.is_slotted
689689
self._frozen = props.is_frozen
690690
self._weakref_slot = props.has_weakref_slot
691-
self._cache_hash = props.hash is ClassProps.Hashability.HASHABLE_CACHED
691+
self._cache_hash = (
692+
props.hashability is ClassProps.Hashability.HASHABLE_CACHED
693+
)
692694
self._has_pre_init = bool(getattr(cls, "__attrs_pre_init__", False))
693695
self._pre_init_has_args = False
694696
if self._has_pre_init:
@@ -700,7 +702,7 @@ def __init__(
700702
self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False))
701703
self._delete_attribs = not bool(these)
702704
self._is_exc = props.is_exception
703-
self._on_setattr = props.on_setattr
705+
self._on_setattr = props.on_setattr_hook
704706

705707
self._has_custom_setattr = has_custom_setattr
706708
self._wrote_own_setattr = False
@@ -740,7 +742,7 @@ def __init__(
740742
# no on_setattr.
741743
self._on_setattr = None
742744

743-
if props.getstate_setstate:
745+
if props.is_pickleable:
744746
(
745747
self._cls_dict["__getstate__"],
746748
self._cls_dict["__setstate__"],
@@ -1486,33 +1488,33 @@ def wrap(cls):
14861488
is_frozen=is_frozen,
14871489
is_slotted=slots,
14881490
collect_by_mro=collect_by_mro,
1489-
init=_determine_whether_to_implement(
1491+
has_init=_determine_whether_to_implement(
14901492
cls, init, auto_detect, ("__init__",)
14911493
),
1492-
repr=_determine_whether_to_implement(
1494+
has_repr=_determine_whether_to_implement(
14931495
cls, repr, auto_detect, ("__repr__",)
14941496
),
1495-
eq=eq,
1496-
order=not is_exc
1497+
has_eq=eq,
1498+
is_orderable=not is_exc
14971499
and _determine_whether_to_implement(
14981500
cls,
14991501
order_,
15001502
auto_detect,
15011503
("__lt__", "__le__", "__gt__", "__ge__"),
15021504
),
1503-
hash=hashability,
1504-
match_args=match_args,
1505+
hashability=hashability,
1506+
can_match=match_args,
15051507
kw_only=kwo,
15061508
has_weakref_slot=weakref_slot,
1507-
str=str,
1508-
getstate_setstate=_determine_whether_to_implement(
1509+
has_str=str,
1510+
is_pickleable=_determine_whether_to_implement(
15091511
cls,
15101512
getstate_setstate,
15111513
auto_detect,
15121514
("__getstate__", "__setstate__"),
15131515
default=slots,
15141516
),
1515-
on_setattr=on_setattr,
1517+
on_setattr_hook=on_setattr,
15161518
field_transformer=field_transformer,
15171519
)
15181520

@@ -1528,26 +1530,26 @@ def wrap(cls):
15281530
has_custom_setattr=has_own_setattr,
15291531
)
15301532

1531-
if props.repr is True:
1533+
if props.has_repr:
15321534
builder.add_repr(repr_ns)
15331535

1534-
if props.str is True:
1536+
if props.has_str:
15351537
builder.add_str()
15361538

1537-
if props.eq is True:
1539+
if props.has_eq:
15381540
builder.add_eq()
1539-
if props.order is True:
1541+
if props.is_orderable:
15401542
builder.add_order()
15411543

15421544
if not frozen:
15431545
builder.add_setattr()
15441546

15451547
if props.is_hashable:
15461548
builder.add_hash()
1547-
elif props.hash is ClassProps.Hashability.UNHASHABLE:
1549+
elif props.hashability is ClassProps.Hashability.UNHASHABLE:
15481550
builder.make_unhashable()
15491551

1550-
if props.init:
1552+
if props.has_init:
15511553
builder.add_init()
15521554
else:
15531555
builder.add_attrs_init()
@@ -2821,29 +2823,29 @@ class ClassProps:
28212823
Whether the class fields are collected by the method resolution
28222824
order (that is, correctly but unlike `dataclasses`).
28232825
2824-
init (bool):
2826+
has_init (bool):
28252827
Whether the class has an *attrs*-generated ``__init__`` method.
28262828
2827-
repr (bool):
2829+
has_repr (bool):
28282830
Whether the class has an *attrs*-generated ``__repr__`` method.
28292831
2830-
eq (bool): Whether the class has an *attrs*-generated equality methods.
2832+
has_eq (bool): Whether the class has an *attrs*-generated equality methods.
28312833
2832-
order (bool):
2834+
is_orderable (bool):
28332835
Whether the class has an *attrs*-generated ordering methods.
28342836
2835-
hash (Hashability): How `hashable <hashing>` the class is.
2837+
hashability (Hashability): How `hashable <hashing>` the class is.
28362838
2837-
match_args (bool): Whether the class supports `match` over its fields.
2839+
can_match (bool): Whether the class supports `match` over its fields.
28382840
2839-
str (bool):
2841+
has_str (bool):
28402842
Whether the class has an *attrs*-generated ``__str__`` method.
28412843
2842-
getstate_setstate (bool):
2844+
is_pickleable (bool):
28432845
Whether the class has *attrs*-generated ``__getstate__`` and
28442846
``__setstate__`` methods for `pickle`.
28452847
2846-
on_setattr (Callable[[Any, Attribute[Any], Any], Any] | None):
2848+
on_setattr_hook (Callable[[Any, Attribute[Any], Any], Any] | None):
28472849
The class's ``__setattr__`` hook.
28482850
28492851
field_transformer (Callable[[Attribute[Any]], Attribute[Any]] | None):
@@ -2889,15 +2891,15 @@ class KeywordOnly(enum.Enum):
28892891
"is_frozen",
28902892
"kw_only",
28912893
"collect_by_mro",
2892-
"init",
2893-
"repr",
2894-
"eq",
2895-
"order",
2896-
"hash",
2897-
"match_args",
2898-
"str",
2899-
"getstate_setstate",
2900-
"on_setattr",
2894+
"has_init",
2895+
"has_repr",
2896+
"has_eq",
2897+
"is_orderable",
2898+
"hashability",
2899+
"can_match",
2900+
"has_str",
2901+
"is_pickleable",
2902+
"on_setattr_hook",
29012903
"field_transformer",
29022904
)
29032905

@@ -2909,15 +2911,15 @@ def __init__(
29092911
is_frozen,
29102912
kw_only,
29112913
collect_by_mro,
2912-
init,
2913-
repr,
2914-
eq,
2915-
order,
2916-
hash,
2917-
match_args,
2918-
str,
2919-
getstate_setstate,
2920-
on_setattr,
2914+
has_init,
2915+
has_repr,
2916+
has_eq,
2917+
is_orderable,
2918+
hashability,
2919+
can_match,
2920+
has_str,
2921+
is_pickleable,
2922+
on_setattr_hook,
29212923
field_transformer,
29222924
):
29232925
self.is_exception = is_exception
@@ -2926,22 +2928,22 @@ def __init__(
29262928
self.is_frozen = is_frozen
29272929
self.kw_only = kw_only
29282930
self.collect_by_mro = collect_by_mro
2929-
self.init = init
2930-
self.repr = repr
2931-
self.eq = eq
2932-
self.order = order
2933-
self.hash = hash
2934-
self.match_args = match_args
2935-
self.str = str
2936-
self.getstate_setstate = getstate_setstate
2937-
self.on_setattr = on_setattr
2931+
self.has_init = has_init
2932+
self.has_repr = has_repr
2933+
self.has_eq = has_eq
2934+
self.is_orderable = is_orderable
2935+
self.hashability = hashability
2936+
self.can_match = can_match
2937+
self.has_str = has_str
2938+
self.is_pickleable = is_pickleable
2939+
self.on_setattr_hook = on_setattr_hook
29382940
self.field_transformer = field_transformer
29392941

29402942
@property
29412943
def is_hashable(self):
29422944
return (
2943-
self.hash is ClassProps.Hashability.HASHABLE
2944-
or self.hash is ClassProps.Hashability.HASHABLE_CACHED
2945+
self.hashability is ClassProps.Hashability.HASHABLE
2946+
or self.hashability is ClassProps.Hashability.HASHABLE_CACHED
29452947
)
29462948

29472949

0 commit comments

Comments
 (0)