Return a script of an initializer for *attrs*, a dict of globals, and annotations for the initializer. The globals are required by the generated script.
(
attrs: list[Attribute],
is_frozen: bool,
is_slotted: bool,
call_pre_init: bool,
pre_init_has_args: bool,
call_post_init: bool,
does_cache_hash: bool,
base_attr_map: dict[str, type],
is_exc: bool,
needs_cached_setattr: bool,
has_cls_on_setattr: bool,
method_name: str,
)
| 2152 | |
| 2153 | |
| 2154 | def _attrs_to_init_script( |
| 2155 | attrs: list[Attribute], |
| 2156 | is_frozen: bool, |
| 2157 | is_slotted: bool, |
| 2158 | call_pre_init: bool, |
| 2159 | pre_init_has_args: bool, |
| 2160 | call_post_init: bool, |
| 2161 | does_cache_hash: bool, |
| 2162 | base_attr_map: dict[str, type], |
| 2163 | is_exc: bool, |
| 2164 | needs_cached_setattr: bool, |
| 2165 | has_cls_on_setattr: bool, |
| 2166 | method_name: str, |
| 2167 | ) -> tuple[str, dict, dict]: |
| 2168 | """ |
| 2169 | Return a script of an initializer for *attrs*, a dict of globals, and |
| 2170 | annotations for the initializer. |
| 2171 | |
| 2172 | The globals are required by the generated script. |
| 2173 | """ |
| 2174 | lines = ["self.__attrs_pre_init__()"] if call_pre_init else [] |
| 2175 | |
| 2176 | if needs_cached_setattr: |
| 2177 | lines.append( |
| 2178 | # Circumvent the __setattr__ descriptor to save one lookup per |
| 2179 | # assignment. Note _setattr will be used again below if |
| 2180 | # does_cache_hash is True. |
| 2181 | "_setattr = _cached_setattr_get(self)" |
| 2182 | ) |
| 2183 | |
| 2184 | extra_lines, fmt_setter, fmt_setter_with_converter = _determine_setters( |
| 2185 | is_frozen, is_slotted, base_attr_map |
| 2186 | ) |
| 2187 | lines.extend(extra_lines) |
| 2188 | |
| 2189 | args = [] # Parameters in the definition of __init__ |
| 2190 | pre_init_args = [] # Parameters in the call to __attrs_pre_init__ |
| 2191 | kw_only_args = [] # Used for both 'args' and 'pre_init_args' above |
| 2192 | attrs_to_validate = [] |
| 2193 | |
| 2194 | # This is a dictionary of names to validator and converter callables. |
| 2195 | # Injecting this into __init__ globals lets us avoid lookups. |
| 2196 | names_for_globals = {} |
| 2197 | annotations = {"return": None} |
| 2198 | |
| 2199 | for a in attrs: |
| 2200 | if a.validator: |
| 2201 | attrs_to_validate.append(a) |
| 2202 | |
| 2203 | attr_name = a.name |
| 2204 | has_on_setattr = a.on_setattr is not None or ( |
| 2205 | a.on_setattr is not setters.NO_OP and has_cls_on_setattr |
| 2206 | ) |
| 2207 | # a.alias is set to maybe-mangled attr_name in _ClassBuilder if not |
| 2208 | # explicitly provided |
| 2209 | arg_name = a.alias |
| 2210 | |
| 2211 | has_factory = isinstance(a.default, Factory) |
no test coverage detected