Repository navigation
Fix narrowing information not propagated in assignment and boolean expressions - #11207
Merged
Merged
Conversation
…pressions Fixes python#8925. Consider the following narrowing (more realistically with TypedDicts), and the current outcome: ```py class A: tag: Literal["A"] class B: tag: Literal["B"] abo: A | B | None if abo is not None and abo.tag == "A": reveal_type(abo.tag) # Type is Literal["A"] reveal_type(abo) # Type is A | B ``` The RHS of the comparison correctly takes into account the LHS, and the `abo.tag` expression is correctly narrowed based on it, but this does not propagate upward to then narrow `abo` to `A`. The problem is that `and`/`or/`not`/assignment expressions recurse using the `find_isinstance_check_helper` function, which omits the `propagate_up_typemap_info` calls that its parent function `find_isinstance_check` performs. Fix this by replacing these recursive `find_isinstance_check_helper` calls with `find_isinstance_check`. This might not always be necessary, but I do not think that always propagating upwards should do any harm, anyways.
bluetech
force-pushed
the
narrowing-upwards
branch
from
September 27, 2021 21:34
12166a7 to
f3a37f7
Compare
JukkaL
approved these changes
Oct 1, 2021
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
The fix looks good to me. Thanks!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #8925.
Consider the following narrowing (more realistically with TypedDicts),
and the current outcome:
The RHS of the comparison correctly takes into account the LHS, and the
abo.tagexpression is correctly narrowed based on it, but this doesnot propagate upward to then narrow
abotoA.The problem is that
and/or/not/assignment expressions recurse usingthe
find_isinstance_check_helperfunction, which omits thepropagate_up_typemap_infocalls that its parent functionfind_isinstance_checkperforms.Fix this by replacing these recursive
find_isinstance_check_helpercalls with
find_isinstance_check. This might not always be necessary,but I do not think that always propagating upwards should do any harm,
anyways.
Test Plan
Added a test case, fails before and passes after.