Skip to content

Commit b36c494

Browse files
committed
Make argument names more idiomatic
1 parent 70eecc9 commit b36c494

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

src/attr/_make.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ def _make_init(
21792179
is_exc,
21802180
needs_cached_setattr,
21812181
has_cls_on_setattr,
2182-
attrs_init,
2182+
"__attrs_init__" if attrs_init else "__init__",
21832183
)
21842184
if cls.__module__ in sys.modules:
21852185
# This makes typing.get_type_hints(CLS.__init__) resolve string types.
@@ -2292,39 +2292,37 @@ def fmt_setter_with_converter(
22922292

22932293

22942294
def _attrs_to_init_script(
2295-
attrs,
2296-
frozen,
2297-
slots,
2298-
pre_init,
2299-
pre_init_has_args,
2300-
post_init,
2301-
cache_hash,
2302-
base_attr_map,
2303-
is_exc,
2304-
needs_cached_setattr,
2305-
has_cls_on_setattr,
2306-
attrs_init,
2307-
):
2308-
"""
2309-
Return a script of an initializer for *attrs* and a dict of globals.
2310-
2311-
The globals are expected by the generated script.
2312-
2313-
If *frozen* is True, we cannot set the attributes directly so we use
2314-
a cached ``object.__setattr__``.
2315-
"""
2316-
lines = ["self.__attrs_pre_init__()"] if pre_init else []
2295+
attrs: list[Attribute],
2296+
is_frozen: bool,
2297+
has_slots: bool,
2298+
call_pre_init: bool,
2299+
pre_init_has_args: bool,
2300+
call_post_init: bool,
2301+
does_cache_hash: bool,
2302+
base_attr_map: dict[str, type],
2303+
is_exc: bool,
2304+
needs_cached_setattr: bool,
2305+
has_cls_on_setattr: bool,
2306+
method_name: str,
2307+
) -> tuple[str, dict, dict]:
2308+
"""
2309+
Return a script of an initializer for *attrs*, a dict of globals, and
2310+
annotations for the initializer.
2311+
2312+
The globals are required by the generated script.
2313+
"""
2314+
lines = ["self.__attrs_pre_init__()"] if call_pre_init else []
23172315

23182316
if needs_cached_setattr:
23192317
lines.append(
23202318
# Circumvent the __setattr__ descriptor to save one lookup per
2321-
# assignment.
2322-
# Note _setattr will be used again below if cache_hash is True
2319+
# assignment. Note _setattr will be used again below if
2320+
# does_cache_hash is True.
23232321
"_setattr = _cached_setattr_get(self)"
23242322
)
23252323

23262324
extra_lines, fmt_setter, fmt_setter_with_converter = _determine_setters(
2327-
frozen, slots, base_attr_map
2325+
is_frozen, has_slots, base_attr_map
23282326
)
23292327
lines.extend(extra_lines)
23302328

@@ -2498,25 +2496,25 @@ def _attrs_to_init_script(
24982496
names_for_globals[val_name] = a.validator
24992497
names_for_globals[attr_name] = a
25002498

2501-
if post_init:
2499+
if call_post_init:
25022500
lines.append("self.__attrs_post_init__()")
25032501

2504-
# because this is set only after __attrs_post_init__ is called, a crash
2502+
# Because this is set only after __attrs_post_init__ is called, a crash
25052503
# will result if post-init tries to access the hash code. This seemed
2506-
# preferable to setting this beforehand, in which case alteration to
2507-
# field values during post-init combined with post-init accessing the
2508-
# hash code would result in silent bugs.
2509-
if cache_hash:
2510-
if frozen:
2511-
if slots: # noqa: SIM108
2504+
# preferable to setting this beforehand, in which case alteration to field
2505+
# values during post-init combined with post-init accessing the hash code
2506+
# would result in silent bugs.
2507+
if does_cache_hash:
2508+
if is_frozen:
2509+
if has_slots:
25122510
# if frozen and slots, then _setattr defined above
2513-
init_hash_cache = "_setattr('%s', %s)"
2511+
init_hash_cache = f"_setattr('{_HASH_CACHE_FIELD}', None)"
25142512
else:
25152513
# if frozen and not slots, then _inst_dict defined above
2516-
init_hash_cache = "_inst_dict['%s'] = %s"
2514+
init_hash_cache = f"_inst_dict['{_HASH_CACHE_FIELD}'] = None"
25172515
else:
2518-
init_hash_cache = "self.%s = %s"
2519-
lines.append(init_hash_cache % (_HASH_CACHE_FIELD, "None"))
2516+
init_hash_cache = f"self.{_HASH_CACHE_FIELD} = None"
2517+
lines.append(init_hash_cache)
25202518

25212519
# For exceptions we rely on BaseException.__init__ for proper
25222520
# initialization.
@@ -2540,14 +2538,13 @@ def _attrs_to_init_script(
25402538
) # handle only kwargs and no regular args
25412539
pre_init_args += pre_init_kw_only_args
25422540

2543-
if pre_init and pre_init_has_args:
2541+
if call_pre_init and pre_init_has_args:
25442542
# If pre init method has arguments, pass same arguments as `__init__`
2545-
lines[0] = "self.__attrs_pre_init__(%s)" % pre_init_args
2543+
lines[0] = f"self.__attrs_pre_init__({pre_init_args})"
25462544

25472545
return (
2548-
"def %s(self, %s):\n %s\n"
2546+
f"def {method_name}(self, %s):\n %s\n"
25492547
% (
2550-
("__attrs_init__" if attrs_init else "__init__"),
25512548
args,
25522549
"\n ".join(lines) if lines else "pass",
25532550
),

0 commit comments

Comments
 (0)