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

Fixes __slots__ regression in 0.930, refs #11821 #11824

Merged
merged 3 commits into from
Dec 30, 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: 6 additions & 3 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,12 @@ def add_slots(self,
self._ctx.reason,
)
return
if info.slots is not None or info.names.get('__slots__'):

generated_slots = {attr.name for attr in attributes}
if ((info.slots is not None and info.slots != generated_slots)
or info.names.get('__slots__')):
# This means we have a slots conflict.
# Class explicitly specifies `__slots__` field.
# Class explicitly specifies a different `__slots__` field.
# And `@dataclass(slots=True)` is used.
# In runtime this raises a type error.
self._ctx.api.fail(
Expand All @@ -234,7 +237,7 @@ def add_slots(self,
)
return

info.slots = {attr.name for attr in attributes}
info.slots = generated_slots

def reset_init_only_vars(self, info: TypeInfo, attributes: List[DataclassAttribute]) -> None:
"""Remove init-only vars from the class and reset init var declarations."""
Expand Down
66 changes: 66 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -1456,3 +1456,69 @@ class Other:
__slots__ = ('x',)
x: int
[builtins fixtures/dataclasses.pyi]


[case testSlotsDefinitionWithTwoPasses1]
# flags: --python-version 3.10
# https://github.com/python/mypy/issues/11821
from typing import TypeVar, Protocol, Generic
from dataclasses import dataclass

C = TypeVar("C", bound="Comparable")

class Comparable(Protocol):
pass

V = TypeVar("V", bound=Comparable)

@dataclass(slots=True)
class Node(Generic[V]): # Error was here
data: V
[builtins fixtures/dataclasses.pyi]

[case testSlotsDefinitionWithTwoPasses2]
# flags: --python-version 3.10
from typing import TypeVar, Protocol, Generic
from dataclasses import dataclass

C = TypeVar("C", bound="Comparable")

class Comparable(Protocol):
pass

V = TypeVar("V", bound=Comparable)

@dataclass(slots=True) # Explicit slots are still not ok:
class Node(Generic[V]): # E: "Node" both defines "__slots__" and is used with "slots=True"
__slots__ = ('data',)
data: V
[builtins fixtures/dataclasses.pyi]

[case testSlotsDefinitionWithTwoPasses3]
# flags: --python-version 3.10
from typing import TypeVar, Protocol, Generic
from dataclasses import dataclass

C = TypeVar("C", bound="Comparable")

class Comparable(Protocol):
pass

V = TypeVar("V", bound=Comparable)

@dataclass(slots=True) # Explicit slots are still not ok, even empty ones:
class Node(Generic[V]): # E: "Node" both defines "__slots__" and is used with "slots=True"
__slots__ = ()
data: V
[builtins fixtures/dataclasses.pyi]

[case testSlotsDefinitionWithTwoPasses4]
# flags: --python-version 3.10
import dataclasses as dtc

PublishedMessagesVar = dict[int, 'PublishedMessages']

@dtc.dataclass(frozen=True, slots=True)
class PublishedMessages:
left: int
[builtins fixtures/dataclasses.pyi]