@@ -51,7 +51,7 @@ class User:
51
51
website:str = field(metadata = {
52
52
"marshmallow_field": marshmallow.fields.Url() # Custom marshmallow field
53
53
})
54
- Schema: ClassVar[Type[Schema]] = Schema # For the type checker
54
+ Schema: t. ClassVar[Type[Schema]] = Schema # For the type checker
55
55
"""
56
56
57
57
__all__ = ("schema_class" , "schema" )
@@ -63,19 +63,8 @@ class User:
63
63
import typing as t
64
64
import typing
65
65
import uuid
66
- from enum import Enum
67
- from enum import EnumMeta
68
- from typing import Any
69
- from typing import ClassVar
70
- from typing import Dict
71
- from typing import List
72
- from typing import Mapping
73
- from typing import NewType
74
- from typing import Optional
75
- from typing import Tuple
76
- from typing import Type
77
- from typing import Union
78
- from typing import cast
66
+ import enum
67
+
79
68
import attr
80
69
import marshmallow
81
70
import typing_inspect
@@ -89,7 +78,9 @@ class User:
89
78
T = t .TypeVar ("T" )
90
79
91
80
92
- def class_schema (clazz : type , meta : Dict [str , Any ] = {}) -> Type [marshmallow .Schema ]:
81
+ def class_schema (
82
+ clazz : type , meta : t .Dict [str , t .Any ] = {}
83
+ ) -> t .Type [marshmallow .Schema ]:
93
84
"""
94
85
Convert a class to a marshmallow schema
95
86
:param clazz: A python class (may be a dataclass)
@@ -102,7 +93,7 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
102
93
``marshmallow_field`` key in the metadata dictionary.
103
94
"""
104
95
105
- fields : Union [Tuple [dataclasses .Field ], Tuple [attr .Attribute ]]
96
+ fields : t . Union [t . Tuple [dataclasses .Field ], t . Tuple [attr .Attribute ]]
106
97
107
98
if not isinstance (clazz , type ):
108
99
raise desert .exceptions .UnknownType (
@@ -140,10 +131,10 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
140
131
{** attributes , "Meta" : type ("Meta" , (), meta )},
141
132
)
142
133
143
- return cast (Type [marshmallow .Schema ], cls_schema )
134
+ return t . cast (t . Type [marshmallow .Schema ], cls_schema )
144
135
145
136
146
- _native_to_marshmallow : Dict [type , Type [marshmallow .fields .Field ]] = {
137
+ _native_to_marshmallow : t . Dict [type , t . Type [marshmallow .fields .Field ]] = {
147
138
int : marshmallow .fields .Integer ,
148
139
float : marshmallow .fields .Float ,
149
140
str : marshmallow .fields .String ,
@@ -154,7 +145,7 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
154
145
datetime .date : marshmallow .fields .Date ,
155
146
decimal .Decimal : marshmallow .fields .Decimal ,
156
147
uuid .UUID : marshmallow .fields .UUID ,
157
- Any : marshmallow .fields .Raw ,
148
+ t . Any : marshmallow .fields .Raw ,
158
149
}
159
150
160
151
@@ -172,7 +163,7 @@ def only(items: t.Iterable[T]) -> T:
172
163
173
164
174
165
def field_for_schema (
175
- typ : type , default = marshmallow .missing , metadata : Mapping [str , Any ] = None
166
+ typ : type , default = marshmallow .missing , metadata : t . Mapping [str , t . Any ] = None
176
167
) -> marshmallow .fields .Field :
177
168
"""
178
169
Get a marshmallow Field corresponding to the given python type.
@@ -183,21 +174,21 @@ def field_for_schema(
183
174
184
175
>>> int_field.default
185
176
9
186
- >>> field_for_schema(Dict[str,str]).__class__
177
+ >>> field_for_schema(t. Dict[str,str]).__class__
187
178
<class 'marshmallow.fields.Dict'>
188
- >>> field_for_schema(Optional[str]).__class__
179
+ >>> field_for_schema(t. Optional[str]).__class__
189
180
<class 'marshmallow.fields.String'>
190
181
>>> import marshmallow_enum
191
- >>> field_for_schema(Enum("X", "a b c")).__class__
182
+ >>> field_for_schema(enum. Enum("X", "a b c")).__class__
192
183
<class 'marshmallow_enum.EnumField'>
193
184
>>> import typing
194
185
>>> field_for_schema(typing.Union[int,str]).__class__
195
186
<class 'marshmallow_union.Union'>
196
- >>> field_for_schema(NewType('UserId', int)).__class__
187
+ >>> field_for_schema(t. NewType('UserId', int)).__class__
197
188
<class 'marshmallow.fields.Integer'>
198
- >>> field_for_schema(NewType('UserId', int), default=0).default
189
+ >>> field_for_schema(t. NewType('UserId', int), default=0).default
199
190
0
200
- >>> class Color(Enum):
191
+ >>> class Color(enum. Enum):
201
192
... red = 1
202
193
>>> field_for_schema(Color).__class__
203
194
<class 'marshmallow_enum.EnumField'>
@@ -241,7 +232,7 @@ def field_for_schema(
241
232
if origin :
242
233
arguments = typing_inspect .get_args (typ , True )
243
234
244
- if origin in (list , List ):
235
+ if origin in (list , t . List ):
245
236
field = marshmallow .fields .List (field_for_schema (arguments [0 ]))
246
237
247
238
if origin in (tuple , t .Tuple ) and Ellipsis not in arguments :
@@ -253,7 +244,7 @@ def field_for_schema(
253
244
field = VariadicTuple (
254
245
field_for_schema (only (arg for arg in arguments if arg != Ellipsis ))
255
246
)
256
- elif origin in (dict , Dict ):
247
+ elif origin in (dict , t . Dict ):
257
248
field = marshmallow .fields .Dict (
258
249
keys = field_for_schema (arguments [0 ]),
259
250
values = field_for_schema (arguments [1 ]),
@@ -283,7 +274,7 @@ def field_for_schema(
283
274
field = field_for_schema (newtype_supertype , default = default )
284
275
285
276
# enumerations
286
- if type (typ ) is EnumMeta :
277
+ if type (typ ) is enum . EnumMeta :
287
278
import marshmallow_enum
288
279
289
280
field = marshmallow_enum .EnumField (typ , metadata = metadata )
@@ -312,7 +303,7 @@ def field_for_schema(
312
303
return field
313
304
314
305
315
- def _base_schema (clazz : type ) -> Type [marshmallow .Schema ]:
306
+ def _base_schema (clazz : type ) -> t . Type [marshmallow .Schema ]:
316
307
class BaseSchema (marshmallow .Schema ):
317
308
@marshmallow .post_load
318
309
def make_data_class (self , data , ** _ ):
@@ -321,7 +312,7 @@ def make_data_class(self, data, **_):
321
312
return BaseSchema
322
313
323
314
324
- def _get_field_default (field : Union [dataclasses .Field , attr .Attribute ]):
315
+ def _get_field_default (field : t . Union [dataclasses .Field , attr .Attribute ]):
325
316
"""
326
317
Return a marshmallow default value given a dataclass default value
327
318
>>> _get_field_default(dataclasses.field())
0 commit comments