-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy should support optional tagged unions #8925
Labels
bug
mypy got something wrong
false-positive
mypy gave an error on correct code
priority-1-normal
topic-strict-optional
topic-typed-dict
Comments
JukkaL
added
bug
mypy got something wrong
false-positive
mypy gave an error on correct code
priority-1-normal
topic-strict-optional
topic-typed-dict
labels
Jun 8, 2020
This looks like a bug. |
Closed
bluetech
added a commit
to bluetech/mypy
that referenced
this issue
Sep 27, 2021
…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
added a commit
to bluetech/mypy
that referenced
this issue
Sep 27, 2021
…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.
JukkaL
pushed a commit
that referenced
this issue
Oct 1, 2021
…pressions (#11207) Fixes #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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
mypy got something wrong
false-positive
mypy gave an error on correct code
priority-1-normal
topic-strict-optional
topic-typed-dict
Report Type: Bug
Example: https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050
Mypy does not discriminate
TypedDicts
whenNone
is part of theUnion
.Mypy narrows a
Union
ofTypedDict
andNone
like it does for unions ofTypedDict
s.mypy 0.770
Python 3.7.6
I have not tried master.
This is a small issue because there is an easy workaround of checking for
None
before checking the tag.The text was updated successfully, but these errors were encountered: