MCPcopy
hub / github.com/pydantic/pydantic / test_modify_get_schema_annotated

Function test_modify_get_schema_annotated

tests/test_annotated.py:198–251  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

196
197
198def test_modify_get_schema_annotated() -> None:
199 calls: list[str] = []
200
201 class CustomType:
202 @classmethod
203 def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
204 calls.append('CustomType:before')
205 with pytest.raises(PydanticSchemaGenerationError):
206 handler(source)
207 schema = core_schema.no_info_plain_validator_function(lambda _: CustomType())
208 calls.append('CustomType:after')
209 return schema
210
211 class PydanticMetadata:
212 def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
213 calls.append('PydanticMetadata:before')
214 schema = handler(source)
215 calls.append('PydanticMetadata:after')
216 return schema
217
218 class GroupedMetadataMarker(GroupedMetadata):
219 def __iter__(self) -> Iterator[BaseMetadata]:
220 # no way to actually hook into schema building
221 # so just register when our iter is called
222 calls.append('GroupedMetadataMarker:iter')
223 yield from []
224
225 class _(BaseModel):
226 x: Annotated[CustomType, GroupedMetadataMarker(), PydanticMetadata()]
227
228 # insert_assert(calls)
229 assert calls == [
230 'GroupedMetadataMarker:iter',
231 'PydanticMetadata:before',
232 'CustomType:before',
233 'CustomType:after',
234 'PydanticMetadata:after',
235 ]
236
237 calls.clear()
238
239 class _(BaseModel):
240 x: Annotated[CustomType, PydanticMetadata(), GroupedMetadataMarker()]
241
242 # insert_assert(calls)
243 assert calls == [
244 'GroupedMetadataMarker:iter',
245 'PydanticMetadata:before',
246 'CustomType:before',
247 'CustomType:after',
248 'PydanticMetadata:after',
249 ]
250
251 calls.clear()
252
253
254def test_get_pydantic_core_schema_source_type() -> None:

Callers

nothing calls this directly

Calls 1

clearMethod · 0.80

Tested by

no test coverage detected