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

Fix AttributeError when using generic SerializableType subclass #275

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mashumaro/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init_subclass__(
] = Sentinel.MISSING,
**kwargs: Any,
):
super().__init_subclass__(**kwargs)
if use_annotations is not Sentinel.MISSING:
cls.__use_annotations__ = use_annotations

Expand Down Expand Up @@ -61,6 +62,7 @@ def __init_subclass__(
] = Sentinel.MISSING,
**kwargs: Any,
):
super().__init_subclass__(**kwargs)
if use_annotations is not Sentinel.MISSING:
cls.__use_annotations__ = use_annotations

Expand Down
21 changes: 21 additions & 0 deletions tests/test_generics_pep_695.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from mashumaro import DataClassDictMixin
from mashumaro.mixins.json import DataClassJSONMixin
from mashumaro.types import SerializableType
from tests.entities import MyGenericDataClass, SerializableTypeGenericList


Expand All @@ -18,6 +19,18 @@ class Bar(Foo):
pass


@dataclass
class GenericSerializableType[T](SerializableType):
value: object

def _serialize(self):
return self.value

@classmethod
def _deserialize(cls, value):
return cls(value)


def test_one_generic():
@dataclass
class A[T]:
Expand Down Expand Up @@ -238,3 +251,11 @@ def test_self_referenced_generic_no_max_recursion_error():
assert Bar.from_dict({"x": 42, "y": {"x": 33, "y": None}}) == obj
assert obj.to_json() == '{"x": 42, "y": {"x": 33, "y": null}}'
assert Bar.from_json('{"x": 42, "y": {"x": 33, "y": null}}') == obj


# Regression test for https://github.com/Fatal1ty/mashumaro/issues/274.
def test_generic_serializable_type_getitem():
@dataclass
class DataClass(DataClassDictMixin):
# Simply specializing the type would lead to an AttributeError.
x: GenericSerializableType[int]