From 8aa43835baae8492224456281aac811b3897f84e Mon Sep 17 00:00:00 2001 From: faph Date: Fri, 17 Nov 2023 16:00:06 +0000 Subject: [PATCH] Support annotated pydantic model --- src/py_avro_schema/_schemas.py | 1 + tests/test_pydantic.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/py_avro_schema/_schemas.py b/src/py_avro_schema/_schemas.py index f74299b..f36eedf 100644 --- a/src/py_avro_schema/_schemas.py +++ b/src/py_avro_schema/_schemas.py @@ -872,6 +872,7 @@ class PydanticSchema(RecordSchema): @classmethod def handles_type(cls, py_type: Type) -> bool: """Whether this schema class can represent a given Python class""" + py_type = _type_from_annotated(py_type) return hasattr(py_type, "__fields__") def __init__(self, py_type: Type[pydantic.BaseModel], namespace: Optional[str] = None, options: Option = Option(0)): diff --git a/tests/test_pydantic.py b/tests/test_pydantic.py index 35938a0..d1d06a9 100644 --- a/tests/test_pydantic.py +++ b/tests/test_pydantic.py @@ -38,6 +38,23 @@ class PyType(pydantic.BaseModel): assert_schema(PyType, expected) +def test_class_annotated(): + class PyType(pydantic.BaseModel): + field_a: str + + expected = { + "type": "record", + "name": "PyType", + "fields": [ + { + "name": "field_a", + "type": "string", + } + ], + } + assert_schema(Annotated[PyType, ...], expected) + + def test_string_field_default(): class PyType(pydantic.BaseModel): field_a: str = ""