Skip to content

Checking of starred expressions - #483

Merged
JukkaL merged 17 commits into
python:masterfrom
spkersten:starexpr
Nov 21, 2014
Merged

JukkaL merged 17 commits into
python:masterfrom
spkersten:starexpr

Conversation

@spkersten

Copy link
Copy Markdown
Contributor

This PR brings type checking of starred expressions in assignments (*rest, last = 1,2,3) and as index of for-statements ([head for head, *tail in L]).

In 9986cef, I have replaced the index of for-statements and generator expressions from being a list of Nodes to a single Node (which will be a tuple, of course, when the list would have had more than one element before the change). Consequently, several testcases have changed in that commit, because the AST/parse tree changed a bit.

@spkersten

Copy link
Copy Markdown
Contributor Author

What is currently missing are type annotations for starred expressions.

I propose to follow the same syntax as used for the starred expressions themselves, like:

a, *b = lijst  # type: int, *List[int]
# b has type List[int]

*(p, q), r = rij  # type: *(int, int), int
# p and q have type int

This would introduce a new type node StarType, used only during the semantic analysis phase to assign types to variables.

In addition: Type inference of rvalues from the lvalue is probably not working correctly for nested starred expressions. However, if don't know how to test this. Input is welcome.

@spkersten

Copy link
Copy Markdown
Contributor Author

I've implemented the starred type annotations I proposed in the previous comment.

@JukkaL

JukkaL commented Nov 2, 2014

Copy link
Copy Markdown
Collaborator

Thanks for implementing this! I did a first review pass and it looks excellent.

I'm going to play around with it a bit a look at the diff in more detail.

Comment thread mypy/checker.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails in case of nested star expressions, like a, *(b, *c) = x. I'd like to fix it, but I have trouble coming up with a test case for type inference from lvalues.

I guess it is not very common, though, and not very useful.

@JukkaL

JukkaL commented Nov 12, 2014

Copy link
Copy Markdown
Collaborator

Apologies for the delay, we have my parents visiting from Finland and I haven't been able to keep up with code reviews. Sorry!

@spkersten

Copy link
Copy Markdown
Contributor Author

Take your time.

@JukkaL
JukkaL merged commit f9eb240 into python:master Nov 21, 2014
@JukkaL

JukkaL commented Nov 21, 2014

Copy link
Copy Markdown
Collaborator

I updated the documentation update to reflect the new documentation structure. Once more, thanks for adding this feature!

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

Successfully merging this pull request may close these issues.

2 participants