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

Method construct

src/anthropic/_models.py:296–353  ·  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

294 @classmethod
295 @override
296 def construct( # pyright: ignore[reportIncompatibleMethodOverride]
297 __cls: Type[ModelT],
298 _fields_set: set[str] | None = None,
299 **values: object,
300 ) -> ModelT:
301 m = __cls.__new__(__cls)
302 fields_values: dict[str, object] = {}
303
304 config = get_model_config(__cls)
305 populate_by_name = (
306 config.allow_population_by_field_name
307 if isinstance(config, _ConfigProtocol)
308 else config.get("populate_by_name")
309 )
310
311 if _fields_set is None:
312 _fields_set = set()
313
314 model_fields = get_model_fields(__cls)
315 for name, field in model_fields.items():
316 key = field.alias
317 if key is None or (key not in values and populate_by_name):
318 key = name
319
320 if key in values:
321 fields_values[name] = _construct_field(value=values[key], field=field, key=key)
322 _fields_set.add(name)
323 else:
324 fields_values[name] = field_get_default(field)
325
326 extra_field_type = _get_extra_fields_type(__cls)
327
328 _extra = {}
329 for key, value in values.items():
330 if key not in model_fields:
331 parsed = construct_type(value=value, type_=extra_field_type) if extra_field_type is not None else value
332
333 if PYDANTIC_V1:
334 _fields_set.add(key)
335 fields_values[key] = parsed
336 else:
337 _extra[key] = parsed
338
339 object.__setattr__(m, "__dict__", fields_values)
340
341 if PYDANTIC_V1:
342 # init_private_attributes() does not exist in v2
343 m._init_private_attributes() # type: ignore
344
345 # copied from Pydantic v1's `construct()` method
346 object.__setattr__(m, "__fields_set__", _fields_set)
347 else:
348 # these properties are copied from Pydantic's `model_construct()` method
349 object.__setattr__(m, "__pydantic_private__", None)
350 object.__setattr__(m, "__pydantic_extra__", _extra)
351 object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
352
353 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

Tested by 15

test_basicFunction · 0.36
test_list_nested_modelFunction · 0.36
test_raw_dictionaryFunction · 0.36
test_unknown_fieldsFunction · 0.36
test_aliasesFunction · 0.36
test_optional_listFunction · 0.36