Skip to content
Closed
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
5 changes: 4 additions & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def _infer_constraints(template: Type, actual: Type,
# T :> U2", but they are not equivalent to the constraint solver,
# which never introduces new Union types (it uses join() instead).
if isinstance(template, TypeVarType):
return [Constraint(template.id, direction, actual)]
return [
Constraint(template.id, direction, actual),
Constraint(template.id, SUBTYPE_OF, template.upper_bound),
]

# Now handle the case of either template or actual being a Union.
# For a Union to be a subtype of another type, every item of the Union
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-generic-subtyping.test
Original file line number Diff line number Diff line change
Expand Up @@ -1033,3 +1033,18 @@ x2: X2[str, int]
reveal_type(iter(x2)) # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type([*x2]) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/dict.pyi]


[case testBoundTypeVarCallable]
# flags: --strict-optional
from typing import Callable, Optional, TypeVar

class something: ...

T = TypeVar("T", bound=something)

def raises(opts: Optional[T]) -> T: ...
def assertRaises(cb: Callable[..., object]) -> None: ...

assertRaises(raises)
[builtins fixtures/dict.pyi]