Skip to content
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

Fix narrowing information not propagated in assignment and boolean expressions #11207

Merged
merged 1 commit into from
Oct 1, 2021

Conversation

bluetech
Copy link
Contributor

Description

Fixes #8925.

Consider the following narrowing (more realistically with TypedDicts),
and the current outcome:

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.

Test Plan

Added a test case, fails before and passes after.

…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.
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

The fix looks good to me. Thanks!

@JukkaL JukkaL merged commit 02b9694 into python:master Oct 1, 2021
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.

mypy should support optional tagged unions
2 participants