MCPcopy
hub / github.com/pydantic/pydantic / test_annotated_alias

Function test_annotated_alias

tests/test_annotated.py:172–195  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

170
171@pytest.mark.skipif(sys.version_info < (3, 10), reason='repr different on older versions')
172def test_annotated_alias() -> None:
173 # https://github.com/pydantic/pydantic/issues/2971
174
175 StrAlias = Annotated[str, Field(max_length=3)]
176 IntAlias = Annotated[int, Field(default_factory=lambda: 2)]
177
178 Nested = Annotated[list[StrAlias], Field(description='foo')]
179
180 class MyModel(BaseModel):
181 a: StrAlias = 'abc'
182 b: StrAlias
183 c: IntAlias
184 d: IntAlias
185 e: Nested
186
187 fields_repr = {k: repr(v) for k, v in MyModel.model_fields.items()}
188 assert fields_repr == {
189 'a': "FieldInfo(annotation=str, required=False, default='abc', metadata=[MaxLen(max_length=3)])",
190 'b': 'FieldInfo(annotation=str, required=True, metadata=[MaxLen(max_length=3)])',
191 'c': 'FieldInfo(annotation=int, required=False, default_factory=<lambda>)',
192 'd': 'FieldInfo(annotation=int, required=False, default_factory=<lambda>)',
193 'e': "FieldInfo(annotation=list[Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[MaxLen(max_length=3)])]], required=True, description='foo')",
194 }
195 assert MyModel(b='def', e=['xyz']).model_dump() == dict(a='abc', b='def', c=2, d=2, e=['xyz'])
196
197
198def test_modify_get_schema_annotated() -> None:

Callers

nothing calls this directly

Calls 4

FieldFunction · 0.90
MyModelClass · 0.70
itemsMethod · 0.45
model_dumpMethod · 0.45

Tested by

no test coverage detected