Skip to content

Adds slots=True support for @attr.s #11489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
init = _get_decorator_bool_argument(ctx, 'init', True)
frozen = _get_frozen(ctx, frozen_default)
order = _determine_eq_order(ctx)
slots = _get_decorator_bool_argument(ctx, 'slots', False)

auto_attribs = _get_decorator_optional_bool_argument(ctx, 'auto_attribs', auto_attribs_default)
kw_only = _get_decorator_bool_argument(ctx, 'kw_only', False)
Expand Down Expand Up @@ -302,6 +303,8 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
return

_add_attrs_magic_attribute(ctx, raw_attr_types=[info[attr.name].type for attr in attributes])
if slots:
_add_slots(ctx, attributes)

# Save the attributes so that subclasses can reuse them.
ctx.cls.info.metadata['attrs'] = {
Expand Down Expand Up @@ -727,6 +730,12 @@ def _add_attrs_magic_attribute(ctx: 'mypy.plugin.ClassDefContext',
)


def _add_slots(ctx: 'mypy.plugin.ClassDefContext',
attributes: List[Attribute]) -> None:
# Unlike `@dataclasses.dataclass`, `__slots__` is rewritten here.
ctx.cls.info.slots = {attr.name for attr in attributes}


class MethodAdder:
"""Helper to add methods to a TypeInfo.

Expand Down
30 changes: 30 additions & 0 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1402,3 +1402,33 @@ class A:
reveal_type(A.__attrs_attrs__) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str]]"

[builtins fixtures/attr.pyi]

[case testAttrsClassWithSlots]
import attr

@attr.s(slots=True)
class A:
b: int = attr.ib()

def __attrs_post_init__(self) -> None:
self.b = 1
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.A"

@attr.dataclass(slots=True)
class B:
__slots__ = () # would be replaced
b: int

def __attrs_post_init__(self) -> None:
self.b = 1
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.B"

@attr.dataclass(slots=False)
class C:
__slots__ = () # would not be replaced
b: int

def __attrs_post_init__(self) -> None:
self.b = 1 # E: Trying to assign name "b" that is not in "__slots__" of type "__main__.C"
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.C"
[builtins fixtures/attr.pyi]