Skip to content

Commit abdbea6

Browse files
committed
with_args, kw_only, slots support in >=3.10
1 parent ad4d654 commit abdbea6

File tree

1 file changed

+130
-21
lines changed

1 file changed

+130
-21
lines changed

marshmallow_dataclass/__init__.py

Lines changed: 130 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class User:
3434
})
3535
Schema: ClassVar[Type[Schema]] = Schema # For the type checker
3636
"""
37+
import sys
3738
import collections.abc
3839
import dataclasses
3940
import inspect
@@ -81,49 +82,156 @@ class User:
8182
# Recursion guard for class_schema()
8283
_RECURSION_GUARD = threading.local()
8384

85+
if sys.version_info >= (3, 10):
86+
# match_args, kw_only, slots
87+
88+
@overload
89+
def dataclass(
90+
_cls: Type[_U],
91+
*,
92+
repr: bool = True,
93+
eq: bool = True,
94+
order: bool = False,
95+
unsafe_hash: bool = False,
96+
frozen: bool = False,
97+
match_args: bool = True,
98+
kw_only: bool = False,
99+
slots: bool = False,
100+
base_schema: Optional[Type[marshmallow.Schema]] = None,
101+
cls_frame: Optional[types.FrameType] = None,
102+
) -> Type[_U]:
103+
...
104+
105+
@overload
106+
def dataclass(
107+
*,
108+
repr: bool = True,
109+
eq: bool = True,
110+
order: bool = False,
111+
unsafe_hash: bool = False,
112+
frozen: bool = False,
113+
match_args: bool = True,
114+
kw_only: bool = False,
115+
slots: bool = False,
116+
base_schema: Optional[Type[marshmallow.Schema]] = None,
117+
cls_frame: Optional[types.FrameType] = None,
118+
) -> Callable[[Type[_U]], Type[_U]]:
119+
...
120+
121+
# _cls should never be specified by keyword, so start it with an
122+
# underscore. The presence of _cls is used to detect if this
123+
# decorator is being called with parameters or not.
124+
def dataclass(
125+
_cls: Type[_U] = None,
126+
*,
127+
repr: bool = True,
128+
eq: bool = True,
129+
order: bool = False,
130+
unsafe_hash: bool = False,
131+
frozen: bool = False,
132+
match_args: bool = True,
133+
kw_only: bool = False,
134+
slots: bool = False,
135+
base_schema: Optional[Type[marshmallow.Schema]] = None,
136+
cls_frame: Optional[types.FrameType] = None,
137+
) -> Union[Type[_U], Callable[[Type[_U]], Type[_U]]]:
138+
return _dataclass(
139+
_cls=_cls,
140+
base_schema=base_schema,
141+
cls_frame=cls_frame,
142+
# dataclass passthrough
143+
repr=repr,
144+
eq=eq,
145+
order=order,
146+
unsafe_hash=unsafe_hash,
147+
frozen=frozen,
148+
match_args=match_args,
149+
kw_only=kw_only,
150+
slots=slots,
151+
)
152+
153+
else:
154+
@overload
155+
def dataclass(
156+
_cls: Type[_U],
157+
*,
158+
repr: bool = True,
159+
eq: bool = True,
160+
order: bool = False,
161+
unsafe_hash: bool = False,
162+
frozen: bool = False,
163+
base_schema: Optional[Type[marshmallow.Schema]] = None,
164+
cls_frame: Optional[types.FrameType] = None,
165+
) -> Type[_U]:
166+
...
167+
168+
@overload
169+
def dataclass(
170+
*,
171+
repr: bool = True,
172+
eq: bool = True,
173+
order: bool = False,
174+
unsafe_hash: bool = False,
175+
frozen: bool = False,
176+
base_schema: Optional[Type[marshmallow.Schema]] = None,
177+
cls_frame: Optional[types.FrameType] = None,
178+
) -> Callable[[Type[_U]], Type[_U]]:
179+
...
180+
181+
# _cls should never be specified by keyword, so start it with an
182+
# underscore. The presence of _cls is used to detect if this
183+
# decorator is being called with parameters or not.
184+
def dataclass(
185+
_cls: Type[_U] = None,
186+
*,
187+
repr: bool = True,
188+
eq: bool = True,
189+
order: bool = False,
190+
unsafe_hash: bool = False,
191+
frozen: bool = False,
192+
base_schema: Optional[Type[marshmallow.Schema]] = None,
193+
cls_frame: Optional[types.FrameType] = None,
194+
) -> Union[Type[_U], Callable[[Type[_U]], Type[_U]]]:
195+
return _dataclass(
196+
_cls=_cls,
197+
base_schema=base_schema,
198+
cls_frame=cls_frame,
199+
# dataclass passthrough
200+
repr=repr,
201+
eq=eq,
202+
order=order,
203+
unsafe_hash=unsafe_hash,
204+
frozen=frozen,
205+
)
84206

85207
@overload
86-
def dataclass(
208+
def _dataclass(
87209
_cls: Type[_U],
88210
*,
89-
repr: bool = True,
90-
eq: bool = True,
91-
order: bool = False,
92-
unsafe_hash: bool = False,
93-
frozen: bool = False,
94211
base_schema: Optional[Type[marshmallow.Schema]] = None,
95212
cls_frame: Optional[types.FrameType] = None,
213+
**kwargs,
96214
) -> Type[_U]:
97215
...
98216

99-
100217
@overload
101-
def dataclass(
218+
def _dataclass(
102219
*,
103-
repr: bool = True,
104-
eq: bool = True,
105-
order: bool = False,
106-
unsafe_hash: bool = False,
107-
frozen: bool = False,
108220
base_schema: Optional[Type[marshmallow.Schema]] = None,
109221
cls_frame: Optional[types.FrameType] = None,
222+
**kwargs,
110223
) -> Callable[[Type[_U]], Type[_U]]:
111224
...
112225

113-
114226
# _cls should never be specified by keyword, so start it with an
115227
# underscore. The presence of _cls is used to detect if this
116228
# decorator is being called with parameters or not.
117-
def dataclass(
229+
def _dataclass(
118230
_cls: Type[_U] = None,
119231
*,
120-
repr: bool = True,
121-
eq: bool = True,
122-
order: bool = False,
123-
unsafe_hash: bool = False,
124-
frozen: bool = False,
125232
base_schema: Optional[Type[marshmallow.Schema]] = None,
126233
cls_frame: Optional[types.FrameType] = None,
234+
**kwargs,
127235
) -> Union[Type[_U], Callable[[Type[_U]], Type[_U]]]:
128236
"""
129237
This decorator does the same as dataclasses.dataclass, but also applies :func:`add_schema`.
@@ -152,7 +260,8 @@ def dataclass(
152260
"""
153261
# dataclass's typing doesn't expect it to be called as a function, so ignore type check
154262
dc = dataclasses.dataclass( # type: ignore
155-
_cls, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen
263+
_cls,
264+
**kwargs,
156265
)
157266
if not cls_frame:
158267
current_frame = inspect.currentframe()

0 commit comments

Comments
 (0)