-
-
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
Distinguish between attributes of a class and attributes of its instances #1097
Comments
This is how mypy currently behaves by design, but the behavior is clearly a compromise. To distinguish between class and instance attributes, we could do something like this:
|
I very often hit this limitation. Has been any advancement made on the topic? How can I help making this move forward? Thanks :-) |
One thing you can do to help would be to support PEP 526, in its tracker item at python/typing#258 and its draft at https://github.com/phouse512/peps/commits/pep-0526. And maybe |
There's a proposed new way of annotation class variables: python/typing#258 On pre-3.6 Python it would look like this:
This wouldn't immediately help with the original issue, though, and the original proposal that I discussed above is still relevant. We've made no progress on that. @oleiade If you are interested in trying to implement instance-only attribute detection, I'm happy to give some ideas about how to do it -- or if you want to bounce ideas off me, please go ahead :-) |
Historically classvars was used to to define runtime types of instances with the same name in ORMs, forms, validatiors. As an example
I don't know a details of how hard it could be implemented but at first glance it could be expressed as type without new syntax. UPD: |
@enomado I was actually just recently asking something similar for peewee ORM in coleifer/peewee#1559.
Maybe class User:
name: ClassInstanceVar[str] = CharField(...)
User.name # CharField
User().name # str
So you mean with just: class User:
name: str mypy will infer |
No:
Gives
|
All this issue is about that typing system should
|
Well if you just put For the actual example using |
FWIW, Kythe takes a similar attitude to instance and class variables for cross-referencing source files (and not just for Python) -- both instance and class variables are treated as the same. |
@tuukkamustonen looks like i've found the workaround for now.
This could be Generic with ClassT too, to act exact as Probably we can also mix this with
Btw i'm not sure how to deal with set @gvanrossum, thanks! |
Closing as a duplicate of #240. |
The following typechecks:
even though
X
is not an attribute of the classA
, just an attribute of its instances, and thex = A.X
line causes anAttributeError
when executed.The text was updated successfully, but these errors were encountered: