Skip to content

Cannot generate Avro schema when using Generic Classes #826

@maver1ck

Description

@maver1ck

Describe the bug
I'm trying to generate Avro Schema for model with Generic Class.

As a result I'm getting following error:

ValueError: Type ~T for field additonal_info is unknown. Please check the valid types at https://marcosschroh.github.io/dataclasses-avroschema/fields_specification/#avro-field-and-python-types-summary

To Reproduce

from typing import TypeVar, Generic
from dataclasses_avroschema import AvroModel
import pydantic


T = TypeVar('T')

@pydantic.dataclasses.dataclass
class UserInfoGeneric(Generic[T], AvroModel):
    name: str
    age: int
    additonal_info: T

@pydantic.dataclasses.dataclass
class EmailInfoGeneric(AvroModel):
    email: str
    is_verified: bool

@pydantic.dataclasses.dataclass
class UserEmailInfo(UserInfoGeneric[EmailInfoGeneric], AvroModel):
    pass

print(UserEmailInfo.fake())

print(UserEmailInfo.avro_schema())

Expected behavior

I'd like to see behaviour similar to example where I'm not using Generic classes.

from typing import TypeVar, Generic
from dataclasses_avroschema import AvroModel
import pydantic

@pydantic.dataclasses.dataclass
class EmailInfo(AvroModel):
    email: str
    is_verified: bool

@pydantic.dataclasses.dataclass
class AddressInfo(AvroModel):
    street: str
    city: str
    state: str
    zip: int

@pydantic.dataclasses.dataclass
class UserInfo(AvroModel):
    name: str
    age: int
    additonal_info: AddressInfo | EmailInfo

print(UserInfo.fake())

print(UserInfo.avro_schema())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions