-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Try empty context when assigning to union typed variables #14151
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c8164dc
Try empty context when assigning to union typed variables
ilevkivskyi 3c3b759
Be more careful
ilevkivskyi 05f849c
Handle one more edge case
ilevkivskyi a6c67d3
Address CR (perf)
ilevkivskyi 233d0dc
Some perf optimization
ilevkivskyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check the performance impact of this? It seems that it should be easy to make this faster in many situations.
One idea would be to first infer the type without the context (if the context is a union type), and only if the inferred type is not compatible with the lvalue type (or there is another error), we'd try again and use the lvalue type as context.
Another idea would to check if the rhs is a reference to a simple variable/attribute or a call to a non-generic function/method. We should be able to skip the re-inferring of the type in this case.
If the performance impact is negligible, we don't need to bother. Both assignment statements and union types are very common, so there might be some impact. I'm a little worried that if we have many bug fixes which each introduce, say, a 1% slowdown, we could easily regress performance a lot over time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried to measure, but it is quite small, so it is hard to measure reliably (<0.5% on mypy self-check). But I agree we can still save some time here. I will probably go with the second idea (skipping "trivial" r.h.s.) since this looks like an easy and safe thing.