Skip to content

TypedDict narrowing regression with enum literal #10414

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

Closed
JukkaL opened this issue May 4, 2021 · 0 comments · Fixed by #10415
Closed

TypedDict narrowing regression with enum literal #10414

JukkaL opened this issue May 4, 2021 · 0 comments · Fixed by #10415
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-typed-dict topic-union-types

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented May 4, 2021

The following example started generating a false positive after #9097:

from typing import TypedDict, Literal, Union
from enum import Enum

class E(Enum):
    FOO = "a"
    BAR = "b"

class Foo(TypedDict):
    tag: Literal[E.FOO]
    x: int

class Bar(TypedDict):
    tag: Literal[E.BAR]
    y: int

def f(d: Union[Foo, Bar]) -> None:
    assert d['tag'] == E.FOO
    d['x']  # TypedDict "Bar" has no key "x"

Type narrowing should work and there should be no error reported.

@JukkaL JukkaL added bug mypy got something wrong topic-typed-dict priority-0-high topic-union-types false-positive mypy gave an error on correct code labels May 4, 2021
@JukkaL JukkaL self-assigned this May 4, 2021
JukkaL added a commit that referenced this issue May 4, 2021
A union of enum literals was merged back to the enum type, which broke
type narrowing. Disable merging in this case to work around the
issue.

The fix feels a bit ad hoc. However, I'd rather not spend a lot of
time figuring out a general fix, since this seems like a pretty rare
edge case.

Fixes #10414.
JukkaL added a commit that referenced this issue May 4, 2021
A union of enum literals was merged back to the enum type, which broke
type narrowing. Disable merging in this case to work around the
issue.

The fix feels a bit ad hoc. However, I'd rather not spend a lot of
time figuring out a general fix, since this seems like a pretty rare
edge case.

Fixes #10414.
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-0-high topic-typed-dict topic-union-types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant