Adds a fields-aware `__init__` method to the class. The added `__init__` will be annotated with types vs. all `Any` depending on the plugin settings.
(
self, fields: list[PydanticModelField], config: ModelConfigData, is_settings: bool, is_root_model: bool
)
| 882 | return default |
| 883 | |
| 884 | def add_initializer( |
| 885 | self, fields: list[PydanticModelField], config: ModelConfigData, is_settings: bool, is_root_model: bool |
| 886 | ) -> None: |
| 887 | """Adds a fields-aware `__init__` method to the class. |
| 888 | |
| 889 | The added `__init__` will be annotated with types vs. all `Any` depending on the plugin settings. |
| 890 | """ |
| 891 | if '__init__' in self._cls.info.names and not self._cls.info.names['__init__'].plugin_generated: |
| 892 | return # Don't generate an __init__ if one already exists |
| 893 | |
| 894 | typed = self.plugin_config.init_typed |
| 895 | model_strict = bool(config.strict) |
| 896 | use_alias = not (config.validate_by_name or config.populate_by_name) and config.validate_by_alias is not False |
| 897 | requires_dynamic_aliases = bool(config.has_alias_generator and not config.validate_by_name) |
| 898 | args = self.get_field_arguments( |
| 899 | fields, |
| 900 | typed=typed, |
| 901 | model_strict=model_strict, |
| 902 | requires_dynamic_aliases=requires_dynamic_aliases, |
| 903 | use_alias=use_alias, |
| 904 | is_settings=is_settings, |
| 905 | is_root_model=is_root_model, |
| 906 | force_typevars_invariant=True, |
| 907 | ) |
| 908 | |
| 909 | if is_settings: |
| 910 | base_settings_node = self._api.lookup_fully_qualified(BASESETTINGS_FULLNAME).node |
| 911 | assert isinstance(base_settings_node, TypeInfo) |
| 912 | if '__init__' in base_settings_node.names: |
| 913 | base_settings_init_node = base_settings_node.names['__init__'].node |
| 914 | assert isinstance(base_settings_init_node, FuncDef) |
| 915 | if base_settings_init_node is not None and base_settings_init_node.type is not None: |
| 916 | func_type = base_settings_init_node.type |
| 917 | assert isinstance(func_type, CallableType) |
| 918 | for arg_idx, arg_name in enumerate(func_type.arg_names): |
| 919 | if arg_name is None or arg_name.startswith('__') or not arg_name.startswith('_'): |
| 920 | continue |
| 921 | analyzed_variable_type = self._api.anal_type(func_type.arg_types[arg_idx]) |
| 922 | if analyzed_variable_type is not None and arg_name in ( |
| 923 | '_cli_settings_source', |
| 924 | '_build_sources', |
| 925 | ): |
| 926 | # These arg names are annotated with types explicitly parameterized with `Any`, and as such |
| 927 | # the Any causes issues with --disallow-any-explicit. As a workaround, change |
| 928 | # the Any type (as if the generic type was left unparameterized): |
| 929 | analyzed_variable_type = analyzed_variable_type.accept( |
| 930 | ChangeExplicitTypeOfAny(TypeOfAny.from_omitted_generics) |
| 931 | ) |
| 932 | variable = Var(arg_name, analyzed_variable_type) |
| 933 | args.append(Argument(variable, analyzed_variable_type, None, ARG_OPT)) |
| 934 | |
| 935 | if not self.should_init_forbid_extra(fields, config): |
| 936 | var = Var('kwargs') |
| 937 | args.append(Argument(var, AnyType(TypeOfAny.explicit), None, ARG_STAR2)) |
| 938 | |
| 939 | add_method(self._api, self._cls, '__init__', args=args, return_type=NoneType()) |
| 940 | |
| 941 | def add_model_construct_method( |
no test coverage detected