Skip to content

I can not assign a value to variable of a type built from the unpacking of another type with the new pythong 3.13 generic syntax #18309

Closed
@GENDRAUD

Description

@GENDRAUD

this code with the old generic syntax works perfectly well

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)

<
there is no problem assigning the value ff tp bis of type RK_functionBIS.
ff take a float , a int and a a int as argument. And the RK_functionBIS take as argument tuple(float,int,int) unpacked

But it fails with the new new generic syntax

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[[Unpack[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)

error messagee: 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]

note that in both case in can run my code successfully- I am not completely sure this is a bug. Perhaps I just made a mistake but somebody in stackoverflow told me to report it as a bug:

https://stackoverflow.com/questions/79292914/why-does-unpacking-with-the-new-generic-syntax-in-python-3-13-cause-a-mypy-error?noredirect=1#comment139825314_79292914

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongcrashtopic-pep-695Issues related to PEP 695 syntax

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions