Skip to content

Checking of starred expressions #483

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

Merged
merged 17 commits into from
Nov 21, 2014
Merged

Checking of starred expressions #483

merged 17 commits into from
Nov 21, 2014

Conversation

spkersten
Copy link
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
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
Contributor Author

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

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 2, 2014

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.

else: # index lvalue
# TODO Figure out more precise type context, probably
# based on the type signature of the _set method.
type_parameters.extend(star_rv_types)
Copy link
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
Copy link
Collaborator

JukkaL commented Nov 12, 2014

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
Contributor Author

Take your time.

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

JukkaL commented Nov 21, 2014

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