I am trying to use the new generic type syntax introduced in Python 3.13 for defining type aliases with unpacking (*). While the code executes correctly, mypy raises a type-checking error. The same code works fine when using the old generic syntax.
Here is my code using the old generic syntax, which works both at runtime and with mypy:
from collections.abc import Callable
from typing import TypeAliasType, Unpack
RK_function_args = TypeAliasType("RK_function_args", tuple[float, int])
# Original function type
RK_function = TypeAliasType("RK_function", Callable[[Unpack[RK_function_args]], int])
# Attempted new function type with an additional int argument
RK_functionBIS = TypeAliasType(
"RK_functionBIS", Callable[[Unpack[RK_function_args], int], int]
)
def ff(a: float, b: int, c: int) -> int:
return 2
bis: RK_functionBIS = ff
res: int = bis(1.0, 2, 3) # OK
print(res)
However, when I rewrite this using the new generic syntax from Python 3.13, mypy raises an error:
from collections.abc import Callable
from typing import Unpack
type RK_function_args = tuple[float, int]
type RK_function = Callable[[Unpack[RK_function_args]], int]
type RK_functionBIS = Callable[[*RK_function_args, int], int]
def f(a: float, b: int) -> int:
return 2
def ff(a: float, b: int, c: int) -> int:
return 2
bis: RK_functionBIS = ff
res: int = bis(1.0, 2, 3) # OK
print(res)
main.py:17: error: Incompatible types in assignment (expression has
type “Callable[[float, int, int], int]”, variable has type
“Callable[[VarArg(*tuple[*tuple[float, int], int])], int]”)
[assignment]
3