Skip to content
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

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

Open
GENDRAUD opened this issue Dec 19, 2024 · 3 comments
Labels
bug mypy got something wrong crash topic-pep-695 Issues related to PEP 695 syntax

Comments

@GENDRAUD
Copy link

GENDRAUD commented Dec 19, 2024

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

@GENDRAUD GENDRAUD added the bug mypy got something wrong label Dec 19, 2024
@brianschubert brianschubert added the topic-pep-695 Issues related to PEP 695 syntax label Dec 19, 2024
@sterliakov
Copy link
Contributor

As I commented there, this is not just a bug - mypy crashes with a traceback on given snippet. Using current master (#d33cef) I get:

q.py:16: 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]                                       
q.py:17: error: INTERNAL ERROR -- Please try using mypy master on GitHub:                                                                                                                                                                   
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build                                                                                                                                                     
Please report a bug at https://github.com/python/mypy/issues                                                                                                                                                                                
version: 1.15.0+dev.d33cef8396c456d87db16dce3525ebf431f4b57f                                                                                                                                                                                
Traceback (most recent call last):                                                                                                                                                                                                          
  File "/tmp/.venv/bin/mypy", line 8, in <module>                                                                                                                                                                                           
    sys.exit(console_entry())                                                                                                                                                                                                               
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/main.py", line 114, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/main.py", line 198, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/build.py", line 270, in _build
    graph = dispatch(sources, manager, stdout)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/build.py", line 2938, in dispatch
    process_graph(graph, manager)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/build.py", line 3336, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/build.py", line 3437, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/build.py", line 2310, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checker.py", line 483, in check_first_pass
    self.accept(d)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/nodes.py", line 1364, in accept
    return visitor.visit_assignment_stmt(self)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checker.py", line 3001, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checker.py", line 3179, in check_assignment
    rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, context=rvalue)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checker.py", line 4350, in check_simple_assignment
    rvalue_type = self.expr_checker.accept(
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 5902, in accept
    typ = node.accept(self)
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/nodes.py", line 1984, in accept
    return visitor.visit_call_expr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 484, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 618, in visit_call_expr_inner
    ret_type = self.check_call_expr_with_callee_type(
        callee_type, e, fullname, object_type, member
    )
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 1475, in check_call_expr_with_callee_type
    ret_type, callee_type = self.check_call(
                            ~~~~~~~~~~~~~~~^
        callee_type,
        ^^^^^^^^^^^^
    ...<6 lines>...
        object_type=object_type,
        ^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 1571, in check_call
    return self.check_callable_call(
           ~~~~~~~~~~~~~~~~~~~~~~~~^
        callee,
        ^^^^^^^
    ...<6 lines>...
        object_type,
        ^^^^^^^^^^^^
    )
    ^
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 1798, in check_callable_call
    self.check_argument_types(
    ~~~~~~~~~~~~~~~~~~~~~~~~~^
        arg_types, arg_kinds, args, callee, formal_to_actual, context, object_type=object_type
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/tmp/.venv/lib/python3.13/site-packages/mypy/checkexpr.py", line 2548, in check_argument_types
    assert isinstance(inner_unpacked_type, Instance)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 
q.py:17: : note: use --pdb to drop into pdb

@GENDRAUD
Copy link
Author

@sterliakov what do you mean when you say it is not a bug. It crashes with the new generic syntax. Although i worked with the old one

@JelleZijlstra
Copy link
Member

He said it is not just a bug: it's a crash, which is worse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong crash topic-pep-695 Issues related to PEP 695 syntax
Projects
None yet
Development

No branches or pull requests

4 participants