Repository navigation
Description
Title: Type variable reported unbound when only a ParamSpec alias's return type uses it and its ParamSpec slot is not a ParamSpec
Bug Report
A function whose return type is a generic alias over a ParamSpec, with a parameter list ([int]) or ... in the
ParamSpec slot, gets Type variable "T" is unbound for a type variable that appears only in that return type. With the
type statement the same function gets Name "T" is not defined, although T is declared in its type parameter list.
The same signature is accepted when:
- the return type is written without the alias (
Callable[[int], T]); - the alias has no ParamSpec (
Callable[[int], T]aliased); - the ParamSpec slot holds a ParamSpec;
Talso appears in a parameter.
pyright, pyrefly and ty accept all of them.
To Reproduce
from collections.abc import Callable
from typing import ParamSpec, TypeAlias, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
Alias: TypeAlias = Callable[P, T]
NoParamSpec: TypeAlias = Callable[[int], T]
def plain() -> Callable[[int], T]: # accepted
raise NotImplementedError
def no_paramspec_alias() -> NoParamSpec[T]: # accepted
raise NotImplementedError
def paramspec_alias() -> Alias[[int], T]: # error: Type variable "T" is unbound
raise NotImplementedErrorWith PEP 695 syntax:
from collections.abc import Callable
type Alias[**P, T] = Callable[P, T]
def paramspec_alias[T]() -> Alias[[int], T]: # error: Name "T" is not defined
raise NotImplementedError
def with_paramspec[**Q, T]() -> Alias[Q, T]: # accepted
raise NotImplementedErrortyping_extensions.TypeAliasType("Alias", Callable[P, T], type_params=(P, T)) behaves as the TypeAlias form, and
Alias[..., T] as Alias[[int], T].
Expected Behavior
No errors: paramspec_alias() returns a generic callable def [T] (int) -> T, as plain() does.
Actual Behavior
main.py:19: error: Type variable "main.T" is unbound [valid-type]
main.py:19: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
main.py:19: note: (Hint: Use "T" in function signature to bind "T" inside a function)
and, for the PEP 695 example:
main.py:6: error: Name "T" is not defined [name-defined]
Where it comes from
A decorator-factory alias over a chain type, Dec = Callable[[Chain[P, T]], Chain[P, T2]]. A factory that applies to
any chain and keeps the item type, def identity() -> Dec[..., T, T], hits this; it has to be written with a
ParamSpec of its own, def identity() -> Dec[P, T, T].
Your Environment
- Mypy version used: 2.4.1 (compiled)
- Mypy command-line flags: none (
--strictgives the same errors) - Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.13 (
--python-version 3.13) - For comparison: basedpyright 1.40.2 (pyright 1.1.414), pyrefly 1.4.1 and ty 0.0.99 report no error on either file.
- Not checked on master.
Activity