Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,10 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
return option
satisfiable = []
for c in option:
# TODO: add similar logic for TypeVar values (also in various other places)?
if mypy.subtypes.is_subtype(c.target, c.origin_type_var.upper_bound):
if isinstance(c.origin_type_var, TypeVarType) and c.origin_type_var.values:
if any(mypy.subtypes.is_subtype(c.target, value) for value in c.origin_type_var.values):
satisfiable.append(c)
elif mypy.subtypes.is_subtype(c.target, c.origin_type_var.upper_bound):
satisfiable.append(c)
if not satisfiable:
return None
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,25 @@ def foo(
foo([1])
[builtins fixtures/list.pyi]

[case testGenericUnionMemberWithTypeVarConstraints]

from typing import Generic, TypeVar, Union

T = TypeVar('T', str, int)

class C(Generic[T]): ...

def f(s: Union[T, C[T]]) -> T: ...

ci: C[int]
cs: C[str]

reveal_type(f(1)) # N: Revealed type is "builtins.int"
reveal_type(f('')) # N: Revealed type is "builtins.str"
reveal_type(f(ci)) # N: Revealed type is "builtins.int"
reveal_type(f(cs)) # N: Revealed type is "builtins.str"


[case testNestedInstanceTypeAliasUnsimplifiedUnion]
from typing import TypeVar, Union, Iterator, List, Any
T = TypeVar("T")
Expand Down