@@ -34,11 +34,9 @@ class User:
34
34
})
35
35
Schema: ClassVar[Type[Schema]] = Schema # For the type checker
36
36
"""
37
- import dataclasses
38
37
import inspect
39
- import typing
40
- from functools import lru_cache
41
38
from enum import EnumMeta
39
+ from functools import lru_cache
42
40
from typing import (
43
41
overload ,
44
42
Dict ,
@@ -55,6 +53,7 @@ class User:
55
53
Set ,
56
54
)
57
55
56
+ import dataclasses
58
57
import marshmallow
59
58
import typing_inspect
60
59
@@ -63,9 +62,6 @@ class User:
63
62
NoneType = type (None )
64
63
_U = TypeVar ("_U" )
65
64
66
- T_Schema = TypeVar ("T_Schema" , bound = marshmallow .Schema )
67
- T_SchemaType = Type [T_Schema ]
68
-
69
65
# Whitelist of dataclass members that will be copied to generated schema.
70
66
MEMBERS_WHITELIST : Set [str ] = {"Meta" }
71
67
@@ -165,12 +161,15 @@ def decorator(clazz: Type[_U]) -> Type[_U]:
165
161
return decorator (_cls ) if _cls else decorator
166
162
167
163
168
- @typing .overload
164
+ T_Schema = TypeVar ("T_Schema" , bound = marshmallow .Schema )
165
+ T_SchemaType = Type [T_Schema ]
166
+
167
+ @overload
169
168
def class_schema (clazz : type , base_schema : None = None ) -> Type [marshmallow .Schema ]:
170
169
...
171
170
172
171
173
- @typing . overload
172
+ @overload
174
173
def class_schema (clazz : type , base_schema : T_SchemaType ) -> T_SchemaType :
175
174
...
176
175
@@ -469,12 +468,17 @@ def _base_schema(
469
468
Base schema factory that creates a schema for `clazz` derived either from `base_schema`
470
469
or `BaseSchema`
471
470
"""
471
+
472
472
# Remove `type: ignore` when mypy handles dynamic base classes
473
473
# https://github.com/python/mypy/issues/2813
474
474
class BaseSchema (base_schema or marshmallow .Schema ): # type: ignore
475
- @marshmallow .post_load
476
- def make_data_class (self , data , ** _ ):
477
- return clazz (** data )
475
+ def load (self , data : Mapping , * , many : bool = None , ** kwargs ):
476
+ all_loaded = super ().load (data , many = many , ** kwargs )
477
+ many = self .many if many is None else bool (many )
478
+ if many :
479
+ return [clazz (** loaded ) for loaded in all_loaded ]
480
+ else :
481
+ return clazz (** all_loaded )
478
482
479
483
return BaseSchema
480
484
0 commit comments