Bug Report
Regression in 1.12.
Try to narrowing union between StrEnum (or IntEnum) and Literal no longer works since mypy 1.12.0
To Reproduce Issue can be reproduce with this code sample
from typing_extensions import Literal from typing import Union from enum import StrEnum class Foo(StrEnum): FOO = 'a' def foo(a: Union[Foo, Literal['foo']]) -> Foo: if a == 'foo': return Foo.FOO return a
playground link failing test can be found here
Expected Behavior After the if branch return, variable type should be narrowed to Foo and mypy should not produce error
Actual Behavior After the if branch return, variable type is considered as "Union[Foo, Literal['foo']]" and my
Your Environment
mypy.ini
This bisects to #17866. I'll look into this.
Narrowing has the issue already in 1.11, but the recent changes made the issue more prominent. This doesn't work as expected when using mypy 1.11:
from typing_extensions import Literal from typing import Union class C: def __eq__(self, other): return True def f(x: Union[C, Literal['foo']]) -> None: if x == "foo": reveal_type(x) # Union[__main__.C, Literal['foo']] else: reveal_type(x) # Union[__main__.C, Literal['foo']], but expected just C
Better narrowing with custom equality (python#20643)
Activity
JukkaL commentedon Oct 24, 2024
This bisects to #17866. I'll look into this.
JukkaL commentedon Oct 25, 2024
Narrowing has the issue already in 1.11, but the recent changes made the issue more prominent. This doesn't work as expected when using mypy 1.11:
Better narrowing with custom equality (python#20643)