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

TypeError when creating a marshmallow schema from dataclass inherited from generic class #230

Open
shpindler opened this issue Jan 13, 2023 · 1 comment · May be fixed by #259 or #232
Open

TypeError when creating a marshmallow schema from dataclass inherited from generic class #230

shpindler opened this issue Jan 13, 2023 · 1 comment · May be fixed by #259 or #232
Labels
enhancement New feature or request

Comments

@shpindler
Copy link

It is from my question on StackOverflow: https://stackoverflow.com/questions/75096188/typeerror-when-creating-a-marshmallow-schema-from-dataclass-inherited-from-gener?noredirect=1#comment132530258_75096188

I'm trying to create a marshmallow schema from dataclass which is inherited from generic type:

users_schema = marshmallow_dataclass.class_schema(Users)()

This is my code:

from dataclasses import dataclass
from typing import Generic, List, TypeVar


T = TypeVar("T")


@dataclass
class Pagination(Generic[T]):
    items: List[T]


@dataclass
class User:
    pass


@dataclass
class Users(Pagination[User]):
    pass

However I get this traceback:

src/c1client/entities/schemas.py:39: in <module>
    users_schema = marshmallow_dataclass.class_schema(Users)()
../../../.venvs/cva-user-service/lib/python3.9/site-packages/marshmallow_dataclass/__init__.py:357: in class_schema
    return _internal_class_schema(clazz, base_schema, clazz_frame)
../../../.venvs/cva-user-service/lib/python3.9/site-packages/marshmallow_dataclass/__init__.py:403: in _internal_class_schema
    attributes.update(
../../../.venvs/cva-user-service/lib/python3.9/site-packages/marshmallow_dataclass/__init__.py:406: in <genexpr>
    field_for_schema(
../../../.venvs/cva-user-service/lib/python3.9/site-packages/marshmallow_dataclass/__init__.py:701: in field_for_schema
    generic_field = _field_for_generic_type(typ, base_schema, typ_frame, **metadata)
../../../.venvs/cva-user-service/lib/python3.9/site-packages/marshmallow_dataclass/__init__.py:505: in _field_for_generic_type
    child_type = field_for_schema(
../../../.venvs/cva-user-service/lib/python3.9/site-packages/marshmallow_dataclass/__init__.py:719: in field_for_schema
    if issubclass(typ, Enum):
E   TypeError: issubclass() arg 1 must be a class

When I print typ variable from the traceback it is printed like ~T which is really not a class.

Also when I make Pagination class concrete - not generic, it works. So the problem is related to Generic.

Any ideas what could I do to make it work?

@dairiki
Copy link
Collaborator

dairiki commented Jan 13, 2023

Currently, marshmallow_dataclass does not support generic dataclasses. (This explains why your example does not work.)

PR #172 is now actively working to implement exactly that, so I think support for will be coming "real soon now".

@dairiki dairiki added the enhancement New feature or request label Jan 13, 2023
@dairiki dairiki linked a pull request Jan 21, 2023 that will close this issue
@mvanderlee mvanderlee linked a pull request Jul 7, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants