@@ -2179,7 +2179,7 @@ def _make_init(
2179
2179
is_exc ,
2180
2180
needs_cached_setattr ,
2181
2181
has_cls_on_setattr ,
2182
- attrs_init ,
2182
+ "__attrs_init__" if attrs_init else "__init__" ,
2183
2183
)
2184
2184
if cls .__module__ in sys .modules :
2185
2185
# This makes typing.get_type_hints(CLS.__init__) resolve string types.
@@ -2292,39 +2292,37 @@ def fmt_setter_with_converter(
2292
2292
2293
2293
2294
2294
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 []
2317
2315
2318
2316
if needs_cached_setattr :
2319
2317
lines .append (
2320
2318
# 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.
2323
2321
"_setattr = _cached_setattr_get(self)"
2324
2322
)
2325
2323
2326
2324
extra_lines , fmt_setter , fmt_setter_with_converter = _determine_setters (
2327
- frozen , slots , base_attr_map
2325
+ is_frozen , has_slots , base_attr_map
2328
2326
)
2329
2327
lines .extend (extra_lines )
2330
2328
@@ -2498,25 +2496,25 @@ def _attrs_to_init_script(
2498
2496
names_for_globals [val_name ] = a .validator
2499
2497
names_for_globals [attr_name ] = a
2500
2498
2501
- if post_init :
2499
+ if call_post_init :
2502
2500
lines .append ("self.__attrs_post_init__()" )
2503
2501
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
2505
2503
# 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 :
2512
2510
# 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 )"
2514
2512
else :
2515
2513
# 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 "
2517
2515
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 )
2520
2518
2521
2519
# For exceptions we rely on BaseException.__init__ for proper
2522
2520
# initialization.
@@ -2540,14 +2538,13 @@ def _attrs_to_init_script(
2540
2538
) # handle only kwargs and no regular args
2541
2539
pre_init_args += pre_init_kw_only_args
2542
2540
2543
- if pre_init and pre_init_has_args :
2541
+ if call_pre_init and pre_init_has_args :
2544
2542
# 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 } )"
2546
2544
2547
2545
return (
2548
- "def %s (self, %s):\n %s\n "
2546
+ f "def { method_name } (self, %s):\n %s\n "
2549
2547
% (
2550
- ("__attrs_init__" if attrs_init else "__init__" ),
2551
2548
args ,
2552
2549
"\n " .join (lines ) if lines else "pass" ,
2553
2550
),
0 commit comments