Skip to content

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

Closed
mvitousek opened this issue Nov 12, 2014 · 4 comments
Closed

Annotations for class and object fields #27

mvitousek opened this issue Nov 12, 2014 · 4 comments

Comments

@mvitousek
Copy link

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

class C:
  x = 42

Here there is no annotation to suggest that there would be anything wrong with a statement like C.x='foo'' or C().x='bar', and the programmer might want them to succeed (i.e. C.x has type Any); on the other hand it seems highly desirable to be able to specify that x is an int 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

class D:
  def __init__(self):
    self.x = 42

the field lookup D().x is well-typed and D.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 to Undefined that specify its fields; like

class D:
  x = Field(int)
  def __init__(self):
     self.x = 42

The 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 :)

@JukkaL
Copy link
Contributor

JukkaL commented Nov 21, 2014

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

x could either be a class field or an object field with a default value, but the programmer's intention may not be clear from code. To remain compatible with existing code, mypy always allows both uses.

I guess we could also allow explicitly overriding this for specific fields and just use the hybrid field/attribute type as the default.

@ambv
Copy link
Contributor

ambv commented Jan 7, 2015

Annotating variables will come later, right?

@JukkaL
Copy link
Contributor

JukkaL commented Jan 8, 2015

# type: comments and Undefined(...) also work for attributes.

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).

@ambv
Copy link
Contributor

ambv commented Jan 8, 2015

Okay, so the rules of Undefined, cast and comments apply to instance and class fields, too. Closing this one.

@ambv ambv closed this as completed Jan 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants