| 1995 | |
| 1996 | |
| 1997 | def _make_init_script( |
| 1998 | cls, |
| 1999 | attrs, |
| 2000 | pre_init, |
| 2001 | pre_init_has_args, |
| 2002 | post_init, |
| 2003 | frozen, |
| 2004 | slots, |
| 2005 | cache_hash, |
| 2006 | base_attr_map, |
| 2007 | is_exc, |
| 2008 | cls_on_setattr, |
| 2009 | attrs_init, |
| 2010 | ) -> tuple[str, dict, dict]: |
| 2011 | has_cls_on_setattr = ( |
| 2012 | cls_on_setattr is not None and cls_on_setattr is not setters.NO_OP |
| 2013 | ) |
| 2014 | |
| 2015 | if frozen and has_cls_on_setattr: |
| 2016 | msg = "Frozen classes can't use on_setattr." |
| 2017 | raise ValueError(msg) |
| 2018 | |
| 2019 | needs_cached_setattr = cache_hash or frozen |
| 2020 | filtered_attrs = [] |
| 2021 | attr_dict = {} |
| 2022 | for a in attrs: |
| 2023 | if not a.init and a.default is NOTHING: |
| 2024 | continue |
| 2025 | |
| 2026 | filtered_attrs.append(a) |
| 2027 | attr_dict[a.name] = a |
| 2028 | |
| 2029 | if a.on_setattr is not None: |
| 2030 | if frozen is True and a.on_setattr is not setters.NO_OP: |
| 2031 | msg = "Frozen classes can't use on_setattr." |
| 2032 | raise ValueError(msg) |
| 2033 | |
| 2034 | needs_cached_setattr = True |
| 2035 | elif has_cls_on_setattr and a.on_setattr is not setters.NO_OP: |
| 2036 | needs_cached_setattr = True |
| 2037 | |
| 2038 | script, globs, annotations = _attrs_to_init_script( |
| 2039 | filtered_attrs, |
| 2040 | frozen, |
| 2041 | slots, |
| 2042 | pre_init, |
| 2043 | pre_init_has_args, |
| 2044 | post_init, |
| 2045 | cache_hash, |
| 2046 | base_attr_map, |
| 2047 | is_exc, |
| 2048 | needs_cached_setattr, |
| 2049 | has_cls_on_setattr, |
| 2050 | "__attrs_init__" if attrs_init else "__init__", |
| 2051 | ) |
| 2052 | if cls.__module__ in sys.modules: |
| 2053 | # This makes typing.get_type_hints(CLS.__init__) resolve string types. |
| 2054 | globs.update(sys.modules[cls.__module__].__dict__) |