Skip to content

Commit

Permalink
Support annotated pydantic model
Browse files Browse the repository at this point in the history
  • Loading branch information
faph committed Nov 17, 2023
1 parent 1747a4c commit 8aa4383
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/py_avro_schema/_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down

0 comments on commit 8aa4383

Please sign in to comment.