8000 Adds `slots=True` support for `@dataclass` by sobolevn · Pull Request #11483 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Adds slots=True support for @dataclass #11483

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 6 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Addresses review
  • Loading branch information
sobolevn committed Nov 7, 2021
commit 9c94bde168a23c6ff4e64a871ce50e3e80eeb7cb
4 changes: 3 additions & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ def transform(self) -> None:
def add_slots(self,
info: TypeInfo,
attributes: List[DataclassAttribute],
*,
correct_version: bool) -> None:
if not correct_version:
# This means that version is lower than `3.10`,
# it is just a non-existent argument for `dataclass` function.
self._ctx.api.fail(
'Keyword argument "slots" for "dataclass" is only valid in Python 3.10 and higher',
'Keyword argument "slots" for "dataclass" '
'is only valid in Python 3.10 and higher',
self._ctx.reason,
)
return
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -1411,12 +1411,12 @@ class DynamicDef: # E: "DynamicDef" both defines "__slots__" and is used with "
# flags: --python-version 3.9
from dataclasses import dataclass

@dataclass(slots=True) # E: Unexpected keyword argument "slots" for "dataclass"
@dataclass(slots=True) # E: Keyword argument "slots" for "dataclass" is only valid in Python 3.10 and higher
class Some:
x: int

# Possible conflict:
@dataclass(slots=True) # E: Unexpected keyword argument "slots" for "dataclass"
@dataclass(slots=True) # E: Keyword argument "slots" for "dataclass" is only valid in Python 3.10 and higher
class Other:
__slots__ = ('x',)
x: int
Expand Down
0