MCPcopy
hub / github.com/pydantic/pydantic / for_model

Method for_model

pydantic/_internal/_config.py:100–149  ·  view source on GitHub ↗

Build a new `ConfigWrapper` instance for a `BaseModel`. The config wrapper built based on (in descending order of priority): - options from `kwargs` - options from the `namespace` - options from the base classes (`bases`) Args: bases: A tuple of

(
        cls,
        bases: tuple[type[Any], ...],
        namespace: dict[str, Any],
        raw_annotations: dict[str, Any],
        kwargs: dict[str, Any],
    )

Source from the content-addressed store, hash-verified

98
99 @classmethod
100 def for_model(
101 cls,
102 bases: tuple[type[Any], ...],
103 namespace: dict[str, Any],
104 raw_annotations: dict[str, Any],
105 kwargs: dict[str, Any],
106 ) -> Self:
107 """Build a new `ConfigWrapper` instance for a `BaseModel`.
108
109 The config wrapper built based on (in descending order of priority):
110 - options from `kwargs`
111 - options from the `namespace`
112 - options from the base classes (`bases`)
113
114 Args:
115 bases: A tuple of base classes.
116 namespace: The namespace of the class being created.
117 raw_annotations: The (non-evaluated) annotations of the model.
118 kwargs: The kwargs passed to the class being created.
119
120 Returns:
121 A `ConfigWrapper` instance for `BaseModel`.
122 """
123 config_new = ConfigDict()
124 for base in bases:
125 config = getattr(base, 'model_config', None)
126 if config:
127 config_new.update(config.copy())
128
129 config_class_from_namespace = namespace.get('Config')
130 config_dict_from_namespace = namespace.get('model_config')
131
132 if raw_annotations.get('model_config') and config_dict_from_namespace is None:
133 raise PydanticUserError(
134 '`model_config` cannot be used as a model field name. Use `model_config` for model configuration.',
135 code='model-config-invalid-field-name',
136 )
137
138 if config_class_from_namespace and config_dict_from_namespace:
139 raise PydanticUserError('"Config" and "model_config" cannot be used together', code='config-both')
140
141 config_from_namespace = config_dict_from_namespace or prepare_config(config_class_from_namespace)
142
143 config_new.update(config_from_namespace)
144
145 for k in list(kwargs.keys()):
146 if k in config_keys:
147 config_new[k] = kwargs.pop(k)
148
149 return cls(config_new)
150
151 # we don't show `__getattr__` to type checkers so missing attributes cause errors
152 if not TYPE_CHECKING: # pragma: no branch

Callers 1

__new__Method · 0.80

Calls 7

PydanticUserErrorClass · 0.85
keysMethod · 0.80
prepare_configFunction · 0.70
ConfigDictClass · 0.50
updateMethod · 0.45
copyMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected