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

Method construct

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

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