Skip to content
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

Add runtime __slots__ attribute to dataclasses #15649

Merged
merged 1 commit into from Jul 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/plugins/dataclasses.py
Expand Up @@ -469,9 +469,15 @@ def add_slots(
self._cls,
)
return

info.slots = generated_slots

# Now, insert `.__slots__` attribute to class namespace:
slots_type = TupleType(
[self._api.named_type("builtins.str") for _ in generated_slots],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth creating Literal types here? So the inferred type in the test cases would be tuple[Literal["x"], Literal["y"], Literal["z"]].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because regular classes produce the same result:

class A:
    __slots__ = ('a', 'b')

reveal_type(A.__slots__)  # N: Revealed type is "tuple[builtins.str, builtins.str]"

__match_args__ attribute needs Literal to handle match kwargs correctly, but __slots__ does not need it at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I feel like we might get an issue at some point asking that we use the literal types, but it's fine if we're consistent with how explicit __slots__ works.

self._api.named_type("builtins.tuple"),
)
add_attribute_to_class(self._api, self._cls, "__slots__", slots_type)

def reset_init_only_vars(self, info: TypeInfo, attributes: list[DataclassAttribute]) -> None:
"""Remove init-only vars from the class and reset init var declarations."""
for attr in attributes:
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-dataclasses.test
Expand Up @@ -1547,6 +1547,35 @@ class Other:
[builtins fixtures/dataclasses.pyi]


[case testDataclassWithSlotsRuntimeAttr]
# flags: --python-version 3.10
from dataclasses import dataclass

@dataclass(slots=True)
class Some:
x: int
y: str
z: bool

reveal_type(Some.__slots__) # N: Revealed type is "Tuple[builtins.str, builtins.str, builtins.str]"

@dataclass(slots=True)
class Other:
x: int
y: str

reveal_type(Other.__slots__) # N: Revealed type is "Tuple[builtins.str, builtins.str]"


@dataclass
class NoSlots:
x: int
y: str

NoSlots.__slots__ # E: "Type[NoSlots]" has no attribute "__slots__"
[builtins fixtures/dataclasses.pyi]


[case testSlotsDefinitionWithTwoPasses1]
# flags: --python-version 3.10
# https://github.com/python/mypy/issues/11821
Expand Down