MCPcopy
hub / github.com/pydantic/pydantic / make_fields_optional

Function make_fields_optional

tests/test_fields.py:427–441  ·  view source on GitHub ↗
(model_cls: type[BaseModel])

Source from the content-addressed store, hash-verified

425
426def test_optional_model_using_asdict() -> None:
427 def make_fields_optional(model_cls: type[BaseModel]) -> type[BaseModel]:
428 new_fields = {}
429
430 for f_name, f_info in model_cls.model_fields.items():
431 f_dct = f_info.asdict()
432 new_fields[f_name] = (
433 Annotated[(Union[f_dct['annotation'], None], *f_dct['metadata'], Field(**f_dct['attributes']))], # noqa: F821
434 None,
435 )
436
437 return create_model(
438 f'{type.__name__}Optional',
439 __base__=model_cls, # (1)!
440 **new_fields,
441 )
442
443 class Model(BaseModel):
444 a: Annotated[int, Field(gt=1)]

Callers 1

Calls 4

FieldFunction · 0.90
create_modelFunction · 0.90
asdictMethod · 0.80
itemsMethod · 0.45

Tested by

no test coverage detected