-
-
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
Don't emit an "x has no attribute y" error if "y" was defined in __slots__ #5941
Comments
Any movement on this? I have some classes using |
A possible workaround is: from typing import TYPE_CHECKING
class Data:
__slots__ = ('x', 'y', 'z')
if TYPE_CHECKING:
x: int
y: int
z: int |
Thanks @ilevkivskyi . I thought about doing that, but unfortunately I'm stuck on python 3.4 for this particular project, and 3.4 doesn't support class annotations. I'm currently setting them in |
Another option on 3.4 is to use type comments: class Data:
__slots__ = ('x', 'y', 'z')
if TYPE_CHECKING:
x = None # type: int
y = None # type: int
z = None # type: int but I am not sure it is better. |
Hi, can I please ask if there's been any development on this? |
@ilevkivskyi's workaround seems pretty good to me. Note you don't even need the |
Python version: 3.5.2
mypy version: 0.630
Bug of Feature request: Feature Request
When defining a class with
__slots__
mypy complains that the instance has no attribute, even if it was defined in__slots__
.Consider the following code:
In this case I would expect
mypy
to accept assigning something toa
(using typeAny
), but still complain about assigning tob
.The docs about
__slots__
state that any iterable can be assigned, and contains the following section:So it may also be interesting to allow typing of the entries in
__slots__
, for example:The docs about
__slots__
don't say anything about ordering of the items. So this should be safe even in older version of Python where dicts did not keep ordering yet.I also came across #1211 and #1325 which both mention
__slots__
but are different enough that this should be a separate issue I think.The text was updated successfully, but these errors were encountered: