Skip to content

Exclude irrelevant members in narrow_declared_type from union overlapping with enum #18897

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

Merged
merged 6 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
]
)
if is_enum_overlapping_union(declared, narrowed):
return original_narrowed
# Quick check before reaching `is_overlapping_types`. If it's enum/literal overlap,
# avoid full expansion and make it faster.
assert isinstance(narrowed, UnionType)
return make_simplified_union(
[narrow_declared_type(declared, x) for x in narrowed.relevant_items()]
)
elif not is_overlapping_types(declared, narrowed, prohibit_none_typevar_overlap=True):
if state.strict_optional:
return UninhabitedType()
Expand Down
30 changes: 30 additions & 0 deletions test-data/unit/check-typeguard.test
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,33 @@ def test() -> None:
return
reveal_type(x) # N: Revealed type is "builtins.list[__main__.C]"
[builtins fixtures/tuple.pyi]

[case testTypeGuardedTypeDoesNotLeak]
# https://github.com/python/mypy/issues/18895
from enum import Enum
from typing import Literal, Union
from typing_extensions import TypeGuard

class Model(str, Enum):
A1 = 'model_a1'
A2 = 'model_a2'
B = 'model_b'

MODEL_A = Literal[Model.A1, Model.A2]
MODEL_B = Literal[Model.B]

def is_model_a(model: str) -> TypeGuard[MODEL_A]:
return True

def is_model_b(model: str) -> TypeGuard[MODEL_B]:
return True

def process_model(model: Union[MODEL_A, MODEL_B]) -> int:
return 42

def handle(model: Model) -> int:
if is_model_a(model) or is_model_b(model):
reveal_type(model) # N: Revealed type is "__main__.Model"
return process_model(model)
return 0
[builtins fixtures/tuple.pyi]
17 changes: 17 additions & 0 deletions test-data/unit/check-typeis.test
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,20 @@ def func(arg: Any) -> None:
if is_dataclass(arg):
reveal_type(arg) # N: Revealed type is "Union[Type[__main__.DataclassInstance], __main__.DataclassInstance]"
[builtins fixtures/tuple.pyi]

[case testTypeIsEnumOverlappingUnionExcludesIrrelevant]
from enum import Enum
from typing import Literal
from typing_extensions import TypeIs

class Model(str, Enum):
A = 'a'
B = 'a'

def is_model_a(model: str) -> TypeIs[Literal[Model.A, "foo"]]:
return True

def handle(model: Model) -> None:
if is_model_a(model):
reveal_type(model) # N: Revealed type is "Literal[__main__.Model.A]"
[builtins fixtures/tuple.pyi]