-
Notifications
You must be signed in to change notification settings - Fork 258
Annotations for class and object fields #27
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
Comments
Early in the development of mypy I had the idea of having a distinction between object and class fields (I think they were called instance variables and class variables). After discussing this with a number of people (including Guido, I believe), I decided to give up on the idea and just treat these equivalent. I can't recall all the arguments, but it primarily boils down to that existing Python code often does not make this explicit. Consider this class: class A:
x = 1
# no other reference to x
I guess we could also allow explicitly overriding this for specific fields and just use the hybrid field/attribute type as the default. |
Annotating variables will come later, right? |
Variable annotations may be added to a future Python version, but they won't be added as part of this PEP (see #9 for more about this). |
Okay, so the rules of Undefined, cast and comments apply to instance and class fields, too. Closing this one. |
Right now it doesn't look like there's a way to specify class and object fields to have types. Of course, the analyzer can see method definitions and use them to create class/object types, but what's the right thing to do with a class definition like
Here there is no annotation to suggest that there would be anything wrong with a statement like
C.x='foo''
orC().x='bar'
, and the programmer might want them to succeed (i.e.C.x
has typeAny
); on the other hand it seems highly desirable to be able to specify thatx
is anint
in some way if that's their expected behavior. The# type :
syntax could work here, but it would be nice to have an alternative for analyzers that don't parse comments.Additionally, having a distinction between class fields and object fields would be nice. This enable analyzers to report that, in a program like
the field lookup
D().x
is well-typed andD.x
might not be. It seems like mypy doesn't have this distinction, though I may be missing a way to do so.Two ways to do this that come to mind: any assignments to
self
in__init__
will be treated as specifying object fields (and a# type :
or similar sort of annotation indicating that it has a non-Any
static type). Or, the classdef could contain calls similar toUndefined
that specify its fields; likeThe advantage of the latter approach is that it lets programmers explicitly decide what the interface to their object is, rather inferring it.
(We currently use a third approach, specifying class/object fields through decorators, but that doesn't seem to fit with the style proposed in the PEP, and I'm not a big fan of it anymore either after seeing the better stuff you've come up with :)
The text was updated successfully, but these errors were encountered: