Skip to content

Narrow types for walrus assignment in if statements#8258

Merged
msullivan merged 3 commits into
python:masterfrom
hauntsaninja:narrow_walrus
Jan 8, 2020
Merged

Narrow types for walrus assignment in if statements#8258
msullivan merged 3 commits into
python:masterfrom
hauntsaninja:narrow_walrus

Conversation

@hauntsaninja

@hauntsaninja hauntsaninja commented Jan 8, 2020

Copy link
Copy Markdown
Collaborator

Fixes #7313
Fixes #7316

@hauntsaninja

Copy link
Copy Markdown
Collaborator Author

Let me know if there's a better way to handle the partial types that can result! Or if there are more tests I should be adding. There's a lot that I don't know :-)

I guess one thing I was wondering if there's a way to improve the hint resulting from the partial type (see line 296 on test-data/unit/check-python38.test). Worried that it might be hard for users to realise that you would have to put the annotation on another line.

@JelleZijlstra

Copy link
Copy Markdown
Member

I doubt that a lot of users are going to write if (x := []): (why would you do that except to confuse a typechecker?), so I don't think it's worth it to invest too much in improving the error message. We can revisit that if we get user complaints.

@TH3CHARLie

TH3CHARLie commented Jan 8, 2020

Copy link
Copy Markdown
Collaborator

Will you have an interest in testing against #7313 to see if the PR also happens to fix that issue? (Although I am not sure at all whether these issues come from the same source)

@hauntsaninja

Copy link
Copy Markdown
Collaborator Author

Yes, it fixes #7313 too (updated the PR to mention that). A test exists for that issue on line 269 of test-data/unit/check-python38.test.

Looking through other issues, it does not fix #8236, but I can maybe work on that (in a separate PR).

@msullivan msullivan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems good to me and is really important since this sort of thing is the killer feature of assignment expressions.

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.

PEP 572: Type not narrowed correctly in assignment PEP 572: Assignments not narrowed in right-hand side of and

4 participants