-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
__slots__ isn't effective for limiting attributes #10801
Labels
bug
mypy got something wrong
Comments
I don't think mypy has any support for |
6 tasks
emmatyping
pushed a commit
that referenced
this issue
Sep 21, 2021
### Description Fixes #10801 We can now detect assignment that are not matching defined `__slots__`. Example: ```python class A: __slots__ = ('a',) class B(A): __slots__ = ('b',) def __init__(self) -> None: self.a = 1 # ok self.b = 2 # ok self.c = 3 # error b: B reveal_type(b.c) ```
This was referenced Sep 24, 2021
JukkaL
pushed a commit
that referenced
this issue
Nov 9, 2021
tushar-deepsource
pushed a commit
to DeepSourceCorp/mypy
that referenced
this issue
Jan 20, 2022
) Closes python#11487 Refs python#11482 Refs python#10801
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
Thank you for Mypy! It's been great help :) Recently, I want to use
__slots__
in a class definition to limit the attributes. This way I don't accidentally define attributes with similar but different names (like "bar" in one class but "baz" in another) when I copy my code to a new class. But Mypy seems to ignore it completely, even if CPython throws anAttributeError
when executing the code.Originally I just want to prevent definition of attributes outside
__init__
, but Mypy doesn't seem to provide an option to check this, so I thought about__slots__
as a workaround, but unfortunately this failed too. Please point out any probable workarounds for the above issues.To Reproduce
Put the following in "main.py" and first execute with python then check with mypy.
Expected Behavior
Actual Python output:
Expected Mypy output, to be consistent with the Python output above:
Actual Behavior
Actual Mypy output:
Your Environment (flags, configs, and OS are probably irrelevant)
mypy.ini
(and other config files): (None)The text was updated successfully, but these errors were encountered: