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

AttributeError when using PEP 695 syntax SerializableType subclass #274

Closed
Feuermurmel opened this issue Jan 6, 2025 · 2 comments · Fixed by #275
Closed

AttributeError when using PEP 695 syntax SerializableType subclass #274

Feuermurmel opened this issue Jan 6, 2025 · 2 comments · Fixed by #275
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Feuermurmel
Copy link
Contributor

  • mashumaro version: 3.15
  • Python version: 3.12.8
  • Operating System: macOS 14.7.2

Description

SerializableType.__init_subclass__() does not call object.__init_subclass__(), making it impossible to use PEP 695 syntax on subclasses of SerializableType.

What I Did

The following example creates a generic SerializableType subclass using PEP 695 syntax.

from dataclasses import dataclass
from typing import Any

from mashumaro import DataClassDictMixin
from mashumaro.types import SerializableType

@dataclass
class Foo[T](SerializableType):
    @classmethod
    def _deserialize(cls, value: Any) -> Any: ...

class Model(DataClassDictMixin):
    int_foo: Foo[int]

It should be possible to use the generic type using the Foo[int] syntax, but this leads to an AttributeError.

Traceback (most recent call last):
  File "[...]/example.py", line 12, in <module>
    class Model(DataClassDictMixin):
  File "[...]/example.py", line 13, in Model
    int_foo: Foo[int]
             ~~~^^^^^
  File "[...]/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/typing.py", line 395, in inner
    return _caches[func](*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/typing.py", line 1106, in _generic_class_getitem
    for param in cls.__parameters__:
                 ^^^^^^^^^^^^^^^^^^
AttributeError: type object 'Foo' has no attribute '__parameters__'
Exit code: 1

The problem is that __parameters__ on SerializableType does not get initialized, which is probably done by object.__init_subclass__(). SerializableType.__init_subclass__() does not call object.__init_subclass__(), which leads to this problem.

@Fatal1ty Fatal1ty added bug Something isn't working good first issue Good for newcomers labels Jan 6, 2025
@Fatal1ty
Copy link
Owner

Fatal1ty commented Jan 6, 2025

This is interesting 🤔 I don't know why but running super().__init_subclass__() at the beginning of __init_subclass__ indeed fixes this issue.

@Feuermurmel Since you discovered this, would you be willing to become a contributor and prepare a PR? If not, I will do it anyway after the holidays.

@Feuermurmel
Copy link
Contributor Author

I had a go at creating a PR, please have a look. I tried creating an appropriate test case based on what I found. Please provide feedback or suggestions if you'd like this to be done differently. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants