Skip to content

dataclasses plugins does not respect slots=True argument #11482

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

Closed
sobolevn opened this issue Nov 6, 2021 · 2 comments · Fixed by #11483
Closed

dataclasses plugins does not respect slots=True argument #11482

sobolevn opened this issue Nov 6, 2021 · 2 comments · Fixed by #11483
Assignees
Labels
bug mypy got something wrong

Comments

@sobolevn
Copy link
Member

sobolevn commented Nov 6, 2021

Since 3.10 now has slots=True argument for @dataclass decorator, we need to support it, since #10864 is merged.

Failing case right now:

from dataclasses import dataclass

@dataclass(slots=True)
class Some:
    x: int

    def __init__(self, x: int) -> None:
        self.x = x  # ok
        self.y = 1  # should be an error, but `mypy` is ok
        # Traceback (most recent call last):
        #   ...
        # AttributeError: 'Some' object has no attribute 'y'

Compare it with regular class, which works fine:

class Some:
    __slots__ = ('x',)

    def __init__(self, x: int) -> None:
        self.x = x  # ok
        self.y = 1  # E: Trying to assign name "y" that is not in "__slots__" of type "ex.Some"

Docs: https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass

Refs #11463

@sobolevn sobolevn added the bug mypy got something wrong label Nov 6, 2021
@sobolevn
Copy link
Member Author

sobolevn commented Nov 6, 2021

I cannot assign myself, but consider me assigned 🙂

@sobolevn
Copy link
Member Author

sobolevn commented Nov 6, 2021

We also need to check for slots confict:

from dataclasses import dataclass

@dataclass(slots=True)
class Some:
    __slots__ = ('x',)
    x: int
# Traceback (most recent call last):
#   ...
# TypeError: Some already specifies __slots__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant