MCPcopy
hub / github.com/pydantic/pydantic / __new__

Method __new__

pydantic/_internal/_model_construction.py:84–280  ·  view source on GitHub ↗

Metaclass for creating Pydantic models. Args: cls_name: The name of the class to be created. bases: The base classes of the class to be created. namespace: The attribute dictionary of the class to be created. __pydantic_generic_metadata__: Met

(
        mcs,
        cls_name: str,
        bases: tuple[type[Any], ...],
        namespace: dict[str, Any],
        __pydantic_generic_metadata__: PydanticGenericMetadata | None = None,
        __pydantic_reset_parent_namespace__: bool = True,
        _create_model_module: str | None = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

82@dataclass_transform(kw_only_default=True, field_specifiers=(PydanticModelField, PydanticModelPrivateAttr, NoInitField))
83class ModelMetaclass(ABCMeta):
84 def __new__(
85 mcs,
86 cls_name: str,
87 bases: tuple[type[Any], ...],
88 namespace: dict[str, Any],
89 __pydantic_generic_metadata__: PydanticGenericMetadata | None = None,
90 __pydantic_reset_parent_namespace__: bool = True,
91 _create_model_module: str | None = None,
92 **kwargs: Any,
93 ) -> type:
94 """Metaclass for creating Pydantic models.
95
96 Args:
97 cls_name: The name of the class to be created.
98 bases: The base classes of the class to be created.
99 namespace: The attribute dictionary of the class to be created.
100 __pydantic_generic_metadata__: Metadata for generic models.
101 __pydantic_reset_parent_namespace__: Reset parent namespace.
102 _create_model_module: The module of the class to be created, if created by `create_model`.
103 **kwargs: Catch-all for any other keyword arguments.
104
105 Returns:
106 The new class created by the metaclass.
107 """
108 # Note `ModelMetaclass` refers to `BaseModel`, but is also used to *create* `BaseModel`, so we rely on the fact
109 # that `BaseModel` itself won't have any bases, but any subclass of it will, to determine whether the `__new__`
110 # call we're in the middle of is for the `BaseModel` class.
111 if bases:
112 raw_annotations: dict[str, Any]
113 if sys.version_info >= (3, 14):
114 if (
115 '__annotations__' in namespace
116 ): # `from __future__ import annotations` was used in the model's module
117 raw_annotations = namespace['__annotations__']
118 else:
119 # See https://docs.python.org/3.14/library/annotationlib.html#using-annotations-in-a-metaclass:
120 from annotationlib import Format, call_annotate_function, get_annotate_from_class_namespace
121
122 if annotate := get_annotate_from_class_namespace(namespace):
123 raw_annotations = call_annotate_function(annotate, format=Format.FORWARDREF)
124 else:
125 raw_annotations = {}
126 else:
127 raw_annotations = namespace.get('__annotations__', {})
128
129 base_field_names, class_vars, base_private_attributes = mcs._collect_bases_data(bases)
130
131 config_wrapper = ConfigWrapper.for_model(bases, namespace, raw_annotations, kwargs)
132 namespace['model_config'] = config_wrapper.config_dict
133 private_attributes = inspect_namespace(
134 namespace, raw_annotations, config_wrapper.ignored_types, class_vars, base_field_names
135 )
136 if private_attributes or base_private_attributes:
137 original_model_post_init = get_model_post_init(namespace, bases)
138 if original_model_post_init is not None:
139 # if there are private attributes and a model_post_init function, we handle both
140
141 @wraps(original_model_post_init)

Callers

nothing calls this directly

Calls 15

inspect_namespaceFunction · 0.85
get_model_post_initFunction · 0.85
import_cached_base_modelFunction · 0.85
parent_frame_namespaceFunction · 0.85
NsResolverClass · 0.85
set_model_fieldsFunction · 0.85
set_model_mocksFunction · 0.85
complete_model_classFunction · 0.85
set_default_hash_funcFunction · 0.85

Tested by

no test coverage detected