You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Whilst experimenting with some Pydantic forward annotations I came across a case which I believe causes py-avro-schema to generate an invalid Avro schema when using base & derived Pydantic models.
Consider a project with 2 files within a Python module folder.
base.py
from __future__ import annotations # inclusion of this line causes a failure
from pydantic import BaseModel
import py_avro_schema as pas
class MyBaseClass(BaseModel):
name: str = ""
@classmethod
def get_avro_schema_json(cls) -> bytes:
return pas.generate(cls)
derived.py
from .base import MyBaseClass
class MyDerivedClass(MyBaseClass):
pass
If the JSON representation of the Avro schema is then retrieved using MyDerivedClass.get_avro_schema_json() I see the results: {"type":"record","name":"MyDerivedClass","fields":[{"name":"name","type":"str","default":""}],"namespace":"Common","doc":"Usage docs: https://docs.pydantic.dev/2.9/concepts/models/"}
If avro.schema.parse() is then used on this, it fails due to the type of the name field being str. If the from __future__ import annotations is removed/commented from base.py, the schema generation proceeds as expected.
(My environment is Python 3.12.0 on Windows, avro==1.12.0, py-avro-schema==3.8.2)
The text was updated successfully, but these errors were encountered:
@faph just to confirm, you'd like me to write another test case (which I expect would currently fail) to illustrate the problem? Within test_pydantic.py I presume? If so then yeah, I'll get on it sometime soon.
(BTW - I couldn't decide if this problem is "caused" by py-avro-schema or whether there was some Pydantic behaviour/issue which leads to the problem I observed. Hopefully that will become clearer as this progresses).
Whilst experimenting with some Pydantic forward annotations I came across a case which I believe causes py-avro-schema to generate an invalid Avro schema when using base & derived Pydantic models.
Consider a project with 2 files within a Python module folder.
base.py
derived.py
If the JSON representation of the Avro schema is then retrieved using
MyDerivedClass.get_avro_schema_json()
I see the results:{"type":"record","name":"MyDerivedClass","fields":[{"name":"name","type":"str","default":""}],"namespace":"Common","doc":"Usage docs: https://docs.pydantic.dev/2.9/concepts/models/"}
If
avro.schema.parse()
is then used on this, it fails due to the type of the name field beingstr
. If thefrom __future__ import annotations
is removed/commented from base.py, the schema generation proceeds as expected.(My environment is Python 3.12.0 on Windows, avro==1.12.0, py-avro-schema==3.8.2)
The text was updated successfully, but these errors were encountered: