[ty] Freshen type variables in recursive calls with a ParamSpec
diff --git a/crates/ty_python_semantic/resources/mdtest/generics/legacy/paramspec.md b/crates/ty_python_semantic/resources/mdtest/generics/legacy/paramspec.md index e7bf43a..71c68be 100644 --- a/crates/ty_python_semantic/resources/mdtest/generics/legacy/paramspec.md +++ b/crates/ty_python_semantic/resources/mdtest/generics/legacy/paramspec.md
@@ -1880,6 +1880,24 @@ OptionalCallback(empty).callback(1) # error: [too-many-positional-arguments] ``` +### Recursive calls with a forwarded parameter list + +A recursive call can choose a new type for `T` while forwarding the callback's parameters. The +caller's `value` still has type `T`, so it cannot be used as an `int`. + +```py +from typing import Callable, ParamSpec, TypeVar + +P = ParamSpec("P") +T = TypeVar("T") + +def f(value: T, n: int, callback: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: + if n: + f(0, n - 1, callback, *args, **kwargs) # ok + # error: [invalid-argument-type] "Expected `int`, found `T@f`" + f(0, value, callback, *args, **kwargs) +``` + ### Forwarded arguments with type-variable bounds When a type variable is bounded by `LiteralString`, string literals are not promoted to `str` when
diff --git a/crates/ty_python_semantic/resources/mdtest/generics/pep695/paramspec.md b/crates/ty_python_semantic/resources/mdtest/generics/pep695/paramspec.md index 85f5268..0bd3bba 100644 --- a/crates/ty_python_semantic/resources/mdtest/generics/pep695/paramspec.md +++ b/crates/ty_python_semantic/resources/mdtest/generics/pep695/paramspec.md
@@ -1680,6 +1680,21 @@ forward(default, lambda x: reveal_type(x)) # revealed: int ``` +### Recursive calls with a forwarded parameter list + +A recursive call can choose a new type for `T` while forwarding the callback's parameters. The +caller's `value` still has type `T`, so it cannot be used as an `int`. + +```py +from typing import Callable + +def f[T, **P](value: T, n: int, callback: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: + if n: + f(0, n - 1, callback, *args, **kwargs) # ok + # error: [invalid-argument-type] "Expected `int`, found `T@f`" + f(0, value, callback, *args, **kwargs) +``` + ### Forwarded arguments with type-variable bounds When a type variable is bounded by `LiteralString`, string literals are not promoted to `str` when
diff --git a/crates/ty_python_semantic/src/types/call/bind.rs b/crates/ty_python_semantic/src/types/call/bind.rs index 1678a90..177a323 100644 --- a/crates/ty_python_semantic/src/types/call/bind.rs +++ b/crates/ty_python_semantic/src/types/call/bind.rs
@@ -3659,17 +3659,6 @@ return; } - // ParamSpec freshening needs relation-side support so `Callable[P, R]` inference doesn't - // quantify away `P` before call specialization can use it. - if self.overloads.iter().any(|overload| { - overload - .signature - .generic_context - .is_some_and(|generic_context| generic_context_has_paramspec(db, generic_context)) - }) { - return; - } - // If any of the overloads being called are generic, we need to "freshen" their generic // contexts. The typevars bound by this generic context might collide with other visible // occurrences of the same generic context. This happens most often when referring to a