Skip to content

Commit 610cfb4

Browse files
author
desert
committed
Use module imports
1 parent d77909f commit 610cfb4

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/desert/_make.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class User:
5151
website:str = field(metadata = {
5252
"marshmallow_field": marshmallow.fields.Url() # Custom marshmallow field
5353
})
54-
Schema: ClassVar[Type[Schema]] = Schema # For the type checker
54+
Schema: t.ClassVar[Type[Schema]] = Schema # For the type checker
5555
"""
5656

5757
__all__ = ("schema_class", "schema")
@@ -63,19 +63,8 @@ class User:
6363
import typing as t
6464
import typing
6565
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+
7968
import attr
8069
import marshmallow
8170
import typing_inspect
@@ -89,7 +78,9 @@ class User:
8978
T = t.TypeVar("T")
9079

9180

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]:
9384
"""
9485
Convert a class to a marshmallow schema
9586
: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
10293
``marshmallow_field`` key in the metadata dictionary.
10394
"""
10495

105-
fields: Union[Tuple[dataclasses.Field], Tuple[attr.Attribute]]
96+
fields: t.Union[t.Tuple[dataclasses.Field], t.Tuple[attr.Attribute]]
10697

10798
if not isinstance(clazz, type):
10899
raise desert.exceptions.UnknownType(
@@ -140,10 +131,10 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
140131
{**attributes, "Meta": type("Meta", (), meta)},
141132
)
142133

143-
return cast(Type[marshmallow.Schema], cls_schema)
134+
return t.cast(t.Type[marshmallow.Schema], cls_schema)
144135

145136

146-
_native_to_marshmallow: Dict[type, Type[marshmallow.fields.Field]] = {
137+
_native_to_marshmallow: t.Dict[type, t.Type[marshmallow.fields.Field]] = {
147138
int: marshmallow.fields.Integer,
148139
float: marshmallow.fields.Float,
149140
str: marshmallow.fields.String,
@@ -154,7 +145,7 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
154145
datetime.date: marshmallow.fields.Date,
155146
decimal.Decimal: marshmallow.fields.Decimal,
156147
uuid.UUID: marshmallow.fields.UUID,
157-
Any: marshmallow.fields.Raw,
148+
t.Any: marshmallow.fields.Raw,
158149
}
159150

160151

@@ -172,7 +163,7 @@ def only(items: t.Iterable[T]) -> T:
172163

173164

174165
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
176167
) -> marshmallow.fields.Field:
177168
"""
178169
Get a marshmallow Field corresponding to the given python type.
@@ -183,21 +174,21 @@ def field_for_schema(
183174
184175
>>> int_field.default
185176
9
186-
>>> field_for_schema(Dict[str,str]).__class__
177+
>>> field_for_schema(t.Dict[str,str]).__class__
187178
<class 'marshmallow.fields.Dict'>
188-
>>> field_for_schema(Optional[str]).__class__
179+
>>> field_for_schema(t.Optional[str]).__class__
189180
<class 'marshmallow.fields.String'>
190181
>>> import marshmallow_enum
191-
>>> field_for_schema(Enum("X", "a b c")).__class__
182+
>>> field_for_schema(enum.Enum("X", "a b c")).__class__
192183
<class 'marshmallow_enum.EnumField'>
193184
>>> import typing
194185
>>> field_for_schema(typing.Union[int,str]).__class__
195186
<class 'marshmallow_union.Union'>
196-
>>> field_for_schema(NewType('UserId', int)).__class__
187+
>>> field_for_schema(t.NewType('UserId', int)).__class__
197188
<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
199190
0
200-
>>> class Color(Enum):
191+
>>> class Color(enum.Enum):
201192
... red = 1
202193
>>> field_for_schema(Color).__class__
203194
<class 'marshmallow_enum.EnumField'>
@@ -241,7 +232,7 @@ def field_for_schema(
241232
if origin:
242233
arguments = typing_inspect.get_args(typ, True)
243234

244-
if origin in (list, List):
235+
if origin in (list, t.List):
245236
field = marshmallow.fields.List(field_for_schema(arguments[0]))
246237

247238
if origin in (tuple, t.Tuple) and Ellipsis not in arguments:
@@ -253,7 +244,7 @@ def field_for_schema(
253244
field = VariadicTuple(
254245
field_for_schema(only(arg for arg in arguments if arg != Ellipsis))
255246
)
256-
elif origin in (dict, Dict):
247+
elif origin in (dict, t.Dict):
257248
field = marshmallow.fields.Dict(
258249
keys=field_for_schema(arguments[0]),
259250
values=field_for_schema(arguments[1]),
@@ -283,7 +274,7 @@ def field_for_schema(
283274
field = field_for_schema(newtype_supertype, default=default)
284275

285276
# enumerations
286-
if type(typ) is EnumMeta:
277+
if type(typ) is enum.EnumMeta:
287278
import marshmallow_enum
288279

289280
field = marshmallow_enum.EnumField(typ, metadata=metadata)
@@ -312,7 +303,7 @@ def field_for_schema(
312303
return field
313304

314305

315-
def _base_schema(clazz: type) -> Type[marshmallow.Schema]:
306+
def _base_schema(clazz: type) -> t.Type[marshmallow.Schema]:
316307
class BaseSchema(marshmallow.Schema):
317308
@marshmallow.post_load
318309
def make_data_class(self, data, **_):
@@ -321,7 +312,7 @@ def make_data_class(self, data, **_):
321312
return BaseSchema
322313

323314

324-
def _get_field_default(field: Union[dataclasses.Field, attr.Attribute]):
315+
def _get_field_default(field: t.Union[dataclasses.Field, attr.Attribute]):
325316
"""
326317
Return a marshmallow default value given a dataclass default value
327318
>>> _get_field_default(dataclasses.field())

0 commit comments

Comments
 (0)