Skip to content

Include tuple fallback in constraints built from tuple types #19100

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 5 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,11 @@ def visit_tuple_type(self, template: TupleType) -> list[Constraint]:
res.extend(
infer_constraints(template_items[i], actual_items[i], self.direction)
)
res.extend(
infer_constraints(
template.partial_fallback, actual.partial_fallback, self.direction
)
)
return res
elif isinstance(actual, AnyType):
return self.infer_against_any(template.items, actual)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -2628,3 +2628,19 @@ def fn(f: Callable[[*tuple[T]], int]) -> Callable[[*tuple[T]], int]: ...
def test(*args: Unpack[tuple[T]]) -> int: ...
reveal_type(fn(test)) # N: Revealed type is "def [T] (T`1) -> builtins.int"
[builtins fixtures/tuple.pyi]

[case testConstraintsIncludeTupleFallback]
from typing import Generic, TypeVar
from typing_extensions import TypeVarTuple, Unpack

T = TypeVar("T")
Ts = TypeVarTuple("Ts")
_FT = TypeVar("_FT", bound=type)

def identity(smth: _FT) -> _FT:
return smth

@identity
class S(tuple[Unpack[Ts]], Generic[T, Unpack[Ts]]):
def f(self, x: T, /) -> T: ...
[builtins fixtures/tuple.pyi]