Skip to content

Commit b3362ba

Browse files
committed
Fix import style and some docstrings, and reuse is_generic_alias instead of duplicating logic
1 parent 7ac088d commit b3362ba

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

marshmallow_dataclass/__init__.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ class User:
4444
import warnings
4545
from enum import Enum
4646
from functools import lru_cache, partial
47-
from typing import Any, Callable, Dict, FrozenSet, Generic, List, Mapping
48-
from typing import NewType as typing_NewType
4947
from typing import (
48+
Any,
49+
Callable,
50+
Dict,
51+
FrozenSet,
52+
Generic,
53+
List,
54+
Mapping,
55+
NewType as typing_NewType,
5056
Optional,
5157
Sequence,
5258
Set,
@@ -146,7 +152,7 @@ def _check_decorated_type(cls: object) -> None:
146152
# A .Schema attribute doesn't make sense on a generic alias — there's
147153
# no way for it to know the generic parameters at run time.
148154
raise TypeError(
149-
"decorator does not support generic aliasses "
155+
"decorator does not support generic aliases "
150156
"(hint: use class_schema directly instead)"
151157
)
152158

@@ -1028,13 +1034,8 @@ def is_generic_alias_of_dataclass(clazz: type) -> bool:
10281034
Check if given class is a generic alias of a dataclass, if the dataclass is
10291035
defined as `class A(Generic[T])`, this method will return true if `A[int]` is passed
10301036
"""
1031-
is_generic = is_generic_type(clazz)
1032-
type_arguments = typing_extensions.get_args(clazz)
1033-
origin_class = typing_extensions.get_origin(clazz)
1034-
return (
1035-
is_generic
1036-
and len(type_arguments) > 0
1037-
and dataclasses.is_dataclass(origin_class)
1037+
return is_generic_alias(clazz) and dataclasses.is_dataclass(
1038+
typing_extensions.get_origin(clazz)
10381039
)
10391040

10401041

@@ -1061,7 +1062,7 @@ def _get_generic_type_hints(
10611062
obj,
10621063
schema_ctx: _SchemaContext,
10631064
) -> type:
1064-
"""typing.get_type_hints doesn't work with generic aliasses. But this 'hack' works."""
1065+
"""typing.get_type_hints doesn't work with generic aliases. But this 'hack' works."""
10651066

10661067
class X:
10671068
x: obj # type: ignore[name-defined]

marshmallow_dataclass/generic_resolver.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def set_result(self, result: _U) -> None:
6969

7070
def is_generic_alias(clazz: type) -> bool:
7171
"""
72-
Check if given class is a generic alias of a class is
73-
defined as `class A(Generic[T])`, this method will return true if `A[int]` is passed
72+
Check if given class is a generic alias of a class.
73+
If a class is defined as `class A(Generic[T])`, this method will return true if `A[int]` is passed
7474
"""
7575
is_generic = is_generic_type(clazz)
7676
type_arguments = typing_extensions.get_args(clazz)
@@ -79,7 +79,7 @@ def is_generic_alias(clazz: type) -> bool:
7979

8080
def is_generic_type(clazz: type) -> bool:
8181
"""
82-
typing_inspect.is_generic_type explicitly ignores Union, Tuple, Callable, ClassVar
82+
typing_inspect.is_generic_type explicitly ignores Union and Tuple
8383
"""
8484
origin = typing_extensions.get_origin(clazz)
8585
return origin is not Annotated and (

tests/test_generics.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ class TestClass(typing.Generic[T, U]):
182182
test_schema.load({"pairs": [("first", "1")]}), TestClass([("first", 1)])
183183
)
184184

185+
def test_deep_generic_with_union(self):
186+
T = typing.TypeVar("T")
187+
U = typing.TypeVar("U")
188+
189+
@dataclasses.dataclass
190+
class TestClass(typing.Generic[T, U]):
191+
either: typing.List[typing.Union[T, U]]
192+
193+
test_schema = class_schema(TestClass[str, int])()
194+
195+
self.assertEqual(
196+
test_schema.load({"either": ["first", 1]}), TestClass(["first", 1])
197+
)
198+
185199
def test_deep_generic_with_overrides(self):
186200
T = typing.TypeVar("T")
187201
U = typing.TypeVar("U")

0 commit comments

Comments
 (0)