Skip to content

Commit 3535666

Browse files
committed
Remove special case for Union type context
Kind of reverts #2715. The test cases in that PR still pass Fixes #20608
1 parent 6424d0b commit 3535666

2 files changed

Lines changed: 19 additions & 28 deletions

File tree

mypy/constraints.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,11 @@ def _infer_constraints(
397397
# be a supertype of the potential subtype, some item of the Union
398398
# must be a supertype of it.
399399
if direction == SUBTYPE_OF and isinstance(actual, UnionType):
400-
# If some of items is not a complete type, disregard that.
401-
items = simplify_away_incomplete_types(actual.items)
402400
# We infer constraints eagerly -- try to find constraints for a type
403401
# variable if possible. This seems to help with some real-world
404402
# use cases.
405403
return any_constraints(
406-
[infer_constraints_if_possible(template, a_item, direction) for a_item in items],
404+
[infer_constraints_if_possible(template, a_item, direction) for a_item in actual.items],
407405
eager=True,
408406
)
409407
if direction == SUPERTYPE_OF and isinstance(template, UnionType):
@@ -652,31 +650,6 @@ def _is_similar_constraints(x: list[Constraint], y: list[Constraint]) -> bool:
652650
return True
653651

654652

655-
def simplify_away_incomplete_types(types: Iterable[Type]) -> list[Type]:
656-
complete = [typ for typ in types if is_complete_type(typ)]
657-
if complete:
658-
return complete
659-
else:
660-
return list(types)
661-
662-
663-
def is_complete_type(typ: Type) -> bool:
664-
"""Is a type complete?
665-
666-
A complete doesn't have uninhabited type components or (when not in strict
667-
optional mode) None components.
668-
"""
669-
return typ.accept(CompleteTypeVisitor())
670-
671-
672-
class CompleteTypeVisitor(BoolTypeQuery):
673-
def __init__(self) -> None:
674-
super().__init__(ALL_STRATEGY)
675-
676-
def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
677-
return False
678-
679-
680653
class ConstraintBuilderVisitor(TypeVisitor[list[Constraint]]):
681654
"""Visitor class for inferring type constraints."""
682655

test-data/unit/check-inference-context.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,24 @@ reveal_type(c.f([])) # N: Revealed type is "builtins.list[builtins.int]"
965965
reveal_type(c.f(None)) # N: Revealed type is "builtins.list[builtins.int] | None"
966966
[builtins fixtures/list.pyi]
967967

968+
[case testUnionWithTupleNeverBranch]
969+
# https://github.com/python/mypy/issues/20608
970+
from __future__ import annotations
971+
from typing import Callable, Never, Generic, TypeVar
972+
973+
T = TypeVar('T')
974+
class ValueType(Generic[T]): pass
975+
class Bundle(Generic[T]): pass
976+
977+
F = TypeVar('F')
978+
979+
def fail_value_type_inference(self_bundle: ValueType[F]) -> tuple[Never, Bundle[ValueType[F]]] | tuple[int, Bundle[ValueType[int]]]:
980+
raise
981+
982+
x = fail_value_type_inference
983+
x = fail_value_type_inference
984+
[builtins fixtures/tuple.pyi]
985+
968986
[case testGenericMethodCalledInGenericContext]
969987
from typing import TypeVar, Generic
970988

0 commit comments

Comments
 (0)