@@ -272,6 +272,7 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
272
272
init = _get_decorator_bool_argument (ctx , 'init' , True )
273
273
frozen = _get_frozen (ctx , frozen_default )
274
274
order = _determine_eq_order (ctx )
275
+ slots = _get_decorator_bool_argument (ctx , 'slots' , False )
275
276
276
277
auto_attribs = _get_decorator_optional_bool_argument (ctx , 'auto_attribs' , auto_attribs_default )
277
278
kw_only = _get_decorator_bool_argument (ctx , 'kw_only' , False )
@@ -302,6 +303,8 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
302
303
return
303
304
304
305
_add_attrs_magic_attribute (ctx , raw_attr_types = [info [attr .name ].type for attr in attributes ])
306
+ if slots :
307
+ _add_slots (ctx , attributes )
305
308
306
309
# Save the attributes so that subclasses can reuse them.
307
310
ctx .cls .info .metadata ['attrs' ] = {
@@ -727,6 +730,12 @@ def _add_attrs_magic_attribute(ctx: 'mypy.plugin.ClassDefContext',
727
730
)
728
731
729
732
733
+ def _add_slots (ctx : 'mypy.plugin.ClassDefContext' ,
734
+ attributes : List [Attribute ]) -> None :
735
+ # Unlike `@dataclasses.dataclass`, `__slots__` is rewritten here.
736
+ ctx .cls .info .slots = {attr .name for attr in attributes }
737
+
738
+
730
739
class MethodAdder :
731
740
"""Helper to add methods to a TypeInfo.
732
741
0 commit comments