MCPcopy
hub / github.com/pydantic/pydantic / test_root_model_field_override

Function test_root_model_field_override

tests/test_fields.py:88–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

86
87
88def test_root_model_field_override():
89 # Weird as this is, I think it's probably best to allow it to ensure it is possible to override
90 # the annotation in subclasses of RootModel subclasses. Basically, I think retaining the flexibility
91 # is worth the increased potential for weird/confusing "accidental" overrides.
92
93 # I'm mostly including this test now to document the behavior
94 class Model(RootModel[int]):
95 root: str
96
97 assert Model.model_validate('abc').root == 'abc'
98 with pytest.raises(ValidationError) as exc_info:
99 Model.model_validate(1)
100 assert exc_info.value.errors(include_url=False) == [
101 {'input': 1, 'loc': (), 'msg': 'Input should be a valid string', 'type': 'string_type'}
102 ]
103
104 class SubModel(Model):
105 root: float
106
107 with pytest.raises(ValidationError) as exc_info:
108 SubModel.model_validate('abc')
109 assert exc_info.value.errors(include_url=False) == [
110 {
111 'input': 'abc',
112 'loc': (),
113 'msg': 'Input should be a valid number, unable to parse string as a number',
114 'type': 'float_parsing',
115 }
116 ]
117
118 validated = SubModel.model_validate_json('1').root
119 assert validated == 1.0
120 assert isinstance(validated, float)
121
122
123def test_frozen_field_repr():

Callers

nothing calls this directly

Calls 3

model_validateMethod · 0.80
model_validate_jsonMethod · 0.80
errorsMethod · 0.45

Tested by

no test coverage detected