Skip to content

Commit d7af73d

Browse files
authored
Merge pull request #1 from kmc6123/add-new-dataclass-kwargs
add slots, match_args, kw_only to dataclass func
2 parents d6396c1 + aebb543 commit d7af73d

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

marshmallow_dataclass/__init__.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def dataclass(
103103
order: bool = False,
104104
unsafe_hash: bool = False,
105105
frozen: bool = False,
106+
match_args: bool = True,
107+
kw_only: bool = False,
108+
slots: bool = True,
106109
base_schema: Optional[Type[marshmallow.Schema]] = None,
107110
cls_frame: Optional[types.FrameType] = None,
108111
) -> Type[_U]:
@@ -117,6 +120,9 @@ def dataclass(
117120
order: bool = False,
118121
unsafe_hash: bool = False,
119122
frozen: bool = False,
123+
match_args: bool = True,
124+
kw_only: bool = False,
125+
slots: bool = True,
120126
base_schema: Optional[Type[marshmallow.Schema]] = None,
121127
cls_frame: Optional[types.FrameType] = None,
122128
) -> Callable[[Type[_U]], Type[_U]]:
@@ -135,6 +141,9 @@ def dataclass(
135141
order: bool = False,
136142
unsafe_hash: bool = False,
137143
frozen: bool = False,
144+
match_args: bool = True,
145+
kw_only: bool = False,
146+
slots: bool = False,
138147
base_schema: Optional[Type[marshmallow.Schema]] = None,
139148
cls_frame: Optional[types.FrameType] = None,
140149
) -> Union[Type[_U], Callable[[Type[_U]], Type[_U]]]:
@@ -163,10 +172,46 @@ def dataclass(
163172
>>> Point.Schema().load({'x':0, 'y':0}) # This line can be statically type checked
164173
Point(x=0.0, y=0.0)
165174
"""
166-
# dataclass's typing doesn't expect it to be called as a function, so ignore type check
167-
dc = dataclasses.dataclass( # type: ignore
168-
_cls, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen
169-
)
175+
# Check python version for dataclass params only available for python >= 3.10.
176+
# If python version is below 3.10 and any of these params are set to anything
177+
# other than their default, raise ValueError
178+
if sys.version_info >= (3, 10):
179+
# dataclass's typing doesn't expect it to be called as a function, so ignore type check
180+
dc = dataclasses.dataclass( # type: ignore
181+
_cls,
182+
repr=repr,
183+
eq=eq,
184+
order=order,
185+
unsafe_hash=unsafe_hash,
186+
frozen=frozen,
187+
match_args=match_args,
188+
kw_only=kw_only,
189+
slots=slots,
190+
)
191+
else:
192+
args = {
193+
'match_args': {
194+
'error_message': "'match_args' argument is only available for python >= 3.10",
195+
'default_value': True
196+
},
197+
'kw_only': {
198+
'error_message': "'kw_only' argument is only available for python >= 3.10",
199+
'default_value': False
200+
},
201+
'slots': {
202+
'error_message': "'slots' argument is only available for python >= 3.10",
203+
'default_value': False
204+
}
205+
}
206+
207+
for arg, arg_info in args.items():
208+
if locals()[arg] is not arg_info['default_value']:
209+
raise ValueError(arg_info['error_message'])
210+
211+
# dataclass's typing doesn't expect it to be called as a function, so ignore type check
212+
dc = dataclasses.dataclass( # type: ignore
213+
_cls, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen
214+
)
170215
if not cls_frame:
171216
current_frame = inspect.currentframe()
172217
if current_frame:

0 commit comments

Comments
 (0)