| 196 | |
| 197 | |
| 198 | def 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 | |
| 254 | def test_get_pydantic_core_schema_source_type() -> None: |