MCPcopy
hub / github.com/anthropics/anthropic-sdk-python / construct

Method construct

src/anthropic/_models.py:221–278  ·  view source on GitHub ↗
(  # pyright: ignore[reportIncompatibleMethodOverride]
        __cls: Type[ModelT],
        _fields_set: set[str] | None = None,
        **values: object,
    )

Source from the content-addressed store, hash-verified

219 @classmethod
220 @override
221 def construct( # pyright: ignore[reportIncompatibleMethodOverride]
222 __cls: Type[ModelT],
223 _fields_set: set[str] | None = None,
224 **values: object,
225 ) -> ModelT:
226 m = __cls.__new__(__cls)
227 fields_values: dict[str, object] = {}
228
229 config = get_model_config(__cls)
230 populate_by_name = (
231 config.allow_population_by_field_name
232 if isinstance(config, _ConfigProtocol)
233 else config.get("populate_by_name")
234 )
235
236 if _fields_set is None:
237 _fields_set = set()
238
239 model_fields = get_model_fields(__cls)
240 for name, field in model_fields.items():
241 key = field.alias
242 if key is None or (key not in values and populate_by_name):
243 key = name
244
245 if key in values:
246 fields_values[name] = _construct_field(value=values[key], field=field, key=key)
247 _fields_set.add(name)
248 else:
249 fields_values[name] = field_get_default(field)
250
251 extra_field_type = _get_extra_fields_type(__cls)
252
253 _extra = {}
254 for key, value in values.items():
255 if key not in model_fields:
256 parsed = construct_type(value=value, type_=extra_field_type) if extra_field_type is not None else value
257
258 if PYDANTIC_V1:
259 _fields_set.add(key)
260 fields_values[key] = parsed
261 else:
262 _extra[key] = parsed
263
264 object.__setattr__(m, "__dict__", fields_values)
265
266 if PYDANTIC_V1:
267 # init_private_attributes() does not exist in v2
268 m._init_private_attributes() # type: ignore
269
270 # copied from Pydantic v1's `construct()` method
271 object.__setattr__(m, "__fields_set__", _fields_set)
272 else:
273 # these properties are copied from Pydantic's `model_construct()` method
274 object.__setattr__(m, "__pydantic_private__", None)
275 object.__setattr__(m, "__pydantic_extra__", _extra)
276 object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
277
278 return m

Callers 15

construct_typeFunction · 0.45
constructMethod · 0.45
getMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
putMethod · 0.45
deleteMethod · 0.45
get_api_listMethod · 0.45
getMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
putMethod · 0.45

Calls 8

get_model_configFunction · 0.85
get_model_fieldsFunction · 0.85
_construct_fieldFunction · 0.85
field_get_defaultFunction · 0.85
_get_extra_fields_typeFunction · 0.85
construct_typeFunction · 0.85
getMethod · 0.45
addMethod · 0.45