-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Narrowing with "tags" on unions (of TypedDicts or normal classes) doesn't work with the match statement #16286
Comments
@AlexWaygood @tmke8 @JelleZijlstra Hi, may I have a try on this issue? |
Yes, feel free! |
Bug report here: python/mypy#16286 There's a PR fix open so maybe "type: ignore" can go away soon.
I'm not sure if I should file this as a separate bug ticket, but I noticed the exact same behavior for normal classes as well, not just TypedDicts. I tweaked the example code from the issue description to demonstrate this, showing that the narrowing works fine for from typing import Literal
class A:
tag: Literal["a"]
name: str
class B:
tag: Literal["b"]
num: int
def f(d: A | B) -> None:
if d.tag == "a":
print(d.name)
elif d.tag == "b":
print(d.num)
def g(d: A | B) -> None:
match d.tag:
case "a":
print(d.name) # E: Item "B" of "A | B" has no attribute "name"
case "b":
print(d.num) # E: Item "A" of "A | B" has no attribute "num" Output:
https://mypy-play.net/?mypy=latest&python=3.12&gist=1b66ee945421120ea4215eaf2ae072a1 |
I can change the title of the issue to reflect the broader scope. |
FYI -- this has just been fixed in pyright 1.1.342:
|
Bug Report
Mypy has a feature where a union of TypedDicts can be narrowed down based on the value of entries: https://mypy.readthedocs.io/en/stable/literal_types.html#tagged-unions
But this doesn't seem to work with pattern matching.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=22cd1c2a185a5182b98e12c1e5dd33e2
Expected Behavior
The narrowing in function
g
should work the same as in functionf
.Actual Behavior
The dictionaries are not correctly narrowed in the match branches.
Your Environment
See mypy-play link above.
The text was updated successfully, but these errors were encountered: