Skip to content

Commit 4d394c1

Browse files
authored
attrs: define defaults to slots=True (#15642)
Fixes #15639. The new-style API `attrs.define` [enables slots by default](https://www.attrs.org/en/stable/api.html#attrs.define).
1 parent 87fa107 commit 4d394c1

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

mypy/plugins/attrs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def attr_class_maker_callback(
294294
ctx: mypy.plugin.ClassDefContext,
295295
auto_attribs_default: bool | None = False,
296296
frozen_default: bool = False,
297+
slots_default: bool = False,
297298
) -> bool:
298299
"""Add necessary dunder methods to classes decorated with attr.s.
299300
@@ -314,7 +315,7 @@ def attr_class_maker_callback(
314315
init = _get_decorator_bool_argument(ctx, "init", True)
315316
frozen = _get_frozen(ctx, frozen_default)
316317
order = _determine_eq_order(ctx)
317-
slots = _get_decorator_bool_argument(ctx, "slots", False)
318+
slots = _get_decorator_bool_argument(ctx, "slots", slots_default)
318319

319320
auto_attribs = _get_decorator_optional_bool_argument(ctx, "auto_attribs", auto_attribs_default)
320321
kw_only = _get_decorator_bool_argument(ctx, "kw_only", False)

mypy/plugins/default.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ def get_class_decorator_hook_2(
159159
attrs.attr_class_maker_callback, auto_attribs_default=None, frozen_default=True
160160
)
161161
elif fullname in attrs.attr_define_makers:
162-
return partial(attrs.attr_class_maker_callback, auto_attribs_default=None)
162+
return partial(
163+
attrs.attr_class_maker_callback, auto_attribs_default=None, slots_default=True
164+
)
163165

164166
return None
165167

test-data/unit/check-plugin-attrs.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,24 @@ reveal_type(A.__attrs_init__) # N: Revealed type is "def (self: __main__.A, b:
16311631
[case testAttrsClassWithSlots]
16321632
import attr
16331633

1634+
@attr.define
1635+
class Define:
1636+
b: int = attr.ib()
1637+
1638+
def __attrs_post_init__(self) -> None:
1639+
self.b = 1
1640+
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.Define"
1641+
1642+
1643+
@attr.define(slots=False)
1644+
class DefineSlotsFalse:
1645+
b: int = attr.ib()
1646+
1647+
def __attrs_post_init__(self) -> None:
1648+
self.b = 1
1649+
self.c = 2
1650+
1651+
16341652
@attr.s(slots=True)
16351653
class A:
16361654
b: int = attr.ib()

0 commit comments

Comments
 (0)