Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema generation failure when using future annotations import #82

Open
gfletcherMerc opened this issue Dec 7, 2024 · 3 comments
Open

Comments

@gfletcherMerc
Copy link

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)

@faph
Copy link
Contributor

faph commented Dec 8, 2024

@gfletcherMerc thanks for raising! Are you able to reproduce this with a unit test at all?

@gfletcherMerc
Copy link
Author

@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).

@faph
Copy link
Contributor

faph commented Dec 17, 2024

@gfletcherMerc yes with a minimal failing unit test it’s easiest to discover where the issue lies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants