Skip to content

How to annotate variables? #9

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
gvanrossum opened this issue Oct 16, 2014 · 8 comments
Closed

How to annotate variables? #9

gvanrossum opened this issue Oct 16, 2014 · 8 comments

Comments

@gvanrossum
Copy link
Member

In mypy there are two ways to annotate a variable: with a # type: comment or a cast expression. The following are equivalent from mypy's POV:

    result = []  # type: List[int]
    result = List[int]()

Each has its disadvantages:

  • Jython's parser doesn't preserve comments and they don't want to change the parser
  • The cast expression incurs significant overhead (List[int] creates a new parametrize type object)

There's no clear solution at this point; something has got to give (though the PEP should probably support both and point out the issues).

For some specific cases (e.g. a variable initialized to an empty concrete container that is used directly in a 'return' statement) we may be able to improve the type inferencer, but in general that's a path we would rather not take (there's a good reason we start with function annotations only).

In the future (e.g. Python 3.6 or later) we may be confident enough to introduce variable annotations, e.g.

var result: List[int] = []

or perhaps even

result: List[int] = []

But this remains to be seen, and we definitely don't want to do this for the current PEP or Python 3.5. Plus, it might still incur exactly the same runtime overhead.

@gvanrossum
Copy link
Member Author

Another option (discussed at our in-person meeting) would be to support # type: comments but give them a lesser status (e.g. "hint") so implementations can ignore them and still be conformant. It's not entirely clear what this would mean -- if the implementation concludes there is a problem because it can't see the comments, is it allowed to issue an error?

@ambv
Copy link
Contributor

ambv commented Jan 7, 2015

I dislike advertising instantiation of typing.List objects because:

  1. it suggests that it's okay to do so for all other typing constructs. Counter-example: TypeVar is special and cannot be freely assigned.
  2. it is yet another way to do it (e.g. newbies will start doing l = List())

Also, if we decide to support registration of alternative implementations (see #7), we need to make sure we don't initialize any typing.List before the registration is done.

As for treating comments as lesser status, I don't think it's pragmatic. The entire reason behind the comments is that otherwise it's impossible to infer the type in time for static analysis.

@gvanrossum
Copy link
Member Author

Agreed -- let's just make List() and List[int]() fail. Depending on whether we endorse # type: ... comments, we can recommend these alternatives (even if they don't always mean exactly the same thing):

x = []  # type: List[int]
x = Undefined(List[int]); x = []
x = cast(List[int], [])

@gvanrossum
Copy link
Member Author

PS: I filed python/mypy#551 -- it seems mypy doesn't flag returning an undefined value as error.

@ambv
Copy link
Contributor

ambv commented Jan 8, 2015

I like using cast() and Undefined in that case. This is exactly what we would want to provide special syntax for in Python 3.6.

Will update the PEP accordingly.

@ambv ambv self-assigned this Jan 8, 2015
@gvanrossum
Copy link
Member Author

Actually, I think the version using a type comment --x = [] # type: List[int] -- should be recommended now that we're endorsing type comments. (#35).

@ambv
Copy link
Contributor

ambv commented Jan 14, 2015

cast is not introduced in the PEP yet.

@ambv
Copy link
Contributor

ambv commented Jan 14, 2015

But the issue is solved in e2e6fc4.

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

2 participants