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

Crash on overlapping check for overloads with Unpack types #17319

Closed
felixblanke opened this issue Jun 3, 2024 · 0 comments · Fixed by #17356
Closed

Crash on overlapping check for overloads with Unpack types #17319

felixblanke opened this issue Jun 3, 2024 · 0 comments · Fixed by #17356
Labels
crash topic-pep-646 PEP 646 (TypeVarTuple, Unpack)

Comments

@felixblanke
Copy link

Crash Report

I ran mypy on a code snippet with two overloads of a function. In those overloads, the arguments are a tuple that starts with the same type for both overloads but is followed by an arbitrary amount of objects of two different types.
Mypy raises an AssertionError when comparing both Unpack objects to check whether the types overlap.

Traceback

$ mypy tmp.py --show-traceback
tmp.py:7: 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.11.0+dev.6c24ea66e20166964aa5d42e28fda5b4b69f44b1
Traceback (most recent call last):
  File "/home/fblanke/miniforge3/envs/mypy_debug/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/main.py", line 103, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/main.py", line 187, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/build.py", line 193, in build
    result = _build(
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/build.py", line 268, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/build.py", line 2950, in dispatch
    process_graph(graph, manager)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/build.py", line 3348, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/build.py", line 3449, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/build.py", line 2314, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 482, in check_first_pass
    self.accept(d)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 590, in accept
    stmt.accept(self)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/nodes.py", line 587, in accept
    return visitor.visit_overloaded_func_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 627, in visit_overloaded_func_def
    self._visit_overloaded_func_def(defn)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 657, in _visit_overloaded_func_def
    self.check_overlapping_overloads(defn)
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 788, in check_overlapping_overloads
    if is_unsafe_overlapping_overload_signatures(sig1, sig2, type_vars):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 7837, in is_unsafe_overlapping_overload_signatures
    return is_callable_compatible(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/subtypes.py", line 1561, in is_callable_compatible
    return are_parameters_compatible(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/subtypes.py", line 1676, in are_parameters_compatible
    if not are_args_compatible(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/subtypes.py", line 1832, in are_args_compatible
    return is_compat(right.typ, left.typ)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/subtypes.py", line 1837, in new_is_compat
    return is_compat(right, left)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/checker.py", line 8366, in is_overlapping_types_no_promote_no_uninhabited_no_none
    return is_overlapping_types(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/meet.py", line 434, in is_overlapping_types
    return are_tuples_overlapping(left, right, ignore_promotions=ignore_promotions)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/meet.py", line 616, in are_tuples_overlapping
    return all(
           ^^^^
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/meet.py", line 617, in <genexpr>
    is_overlapping_types(
  File "/home/fblanke/miniforge3/envs/mypy_debug/lib/python3.12/site-packages/mypy/meet.py", line 550, in is_overlapping_types
    assert type(left) != type(right), f"{type(left)} vs {type(right)}"
           ^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: <class 'mypy.types.UnpackType'> vs <class 'mypy.types.UnpackType'>
tmp.py:7: : note: use --pdb to drop into pdb

To Reproduce

I created a small stand-alone snippet that triggers the crash:

from typing import Union, overload
from typing_extensions import Unpack

tp1 = tuple[int, Unpack[tuple[int, ...]]]
tp2 = tuple[int, Unpack[tuple[str, ...]]]

@overload
def foo(arg: tp1) -> tp1:
    ...

@overload
def foo(arg: tp2) -> tp2:
    ...

def foo(arg: Union[tp1, tp2]) -> Union[tp1, tp2]:
    return arg

Your Environment

  • Mypy version used: mypy 1.11.0+dev.6c24ea66e20166964aa5d42e28fda5b4b69f44b1 (compiled: no)
  • Mypy command-line flags: only --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): ran without a configuration file
  • Python version used: tested on 3.9 and 3.12
  • Operating system and version: Arch linux on kernel 6.9.2-arch1-1
felixblanke added a commit to felixblanke/mypy that referenced this issue Jun 3, 2024
Currently, mypy crashes at comparison of two overloads that use
unpacking on unbounded tuple types. This adds an ad hoc fix
by adding the correspond check in the function and instead
compares the two wrapped inner types that are unpacked.

See mypy issue python#17319:
python#17319
@ilevkivskyi ilevkivskyi added the topic-pep-646 PEP 646 (TypeVarTuple, Unpack) label Jun 3, 2024
ilevkivskyi added a commit that referenced this issue Jun 10, 2024
Fixes #17319

This is still not 100% robust, but at least it should not crash, and
should cover correctly vast majority of cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-pep-646 PEP 646 (TypeVarTuple, Unpack)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants