MCPcopy
hub / github.com/pydantic/pydantic / test_partial_creation_with_defer_build

Function test_partial_creation_with_defer_build

tests/test_config.py:884–905  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

882
883
884def test_partial_creation_with_defer_build():
885 class M(BaseModel):
886 a: int
887 b: int
888
889 def create_partial(model, optionals):
890 override_fields = {}
891 model.model_rebuild()
892 for name, field in model.model_fields.items():
893 if field.is_required() and name in optionals:
894 assert field.annotation is not None
895 override_fields[name] = (Optional[field.annotation], FieldInfo.merge_field_infos(field, default=None))
896
897 return create_model(f'Partial{model.__name__}', __base__=model, **override_fields)
898
899 partial = create_partial(M, {'a'})
900
901 # Comment this away and the last assertion works
902 assert M.model_json_schema()['required'] == ['a', 'b']
903
904 # AssertionError: assert ['a', 'b'] == ['b']
905 assert partial.model_json_schema()['required'] == ['b']
906
907
908def test_model_config_as_model_field_raises():

Callers

nothing calls this directly

Calls 2

create_partialFunction · 0.85
model_json_schemaMethod · 0.45

Tested by

no test coverage detected