Skip to content

Overloaded def including TypeVarTuple and variable length Tuple crash #16427

@robjhornby

Description

@robjhornby

Crash Report

Crash when running mypy from latest master (a1864d4) on the code copied below (note - it does not crash on latest release 1.6.1)

Traceback

Traceback (most recent call last):
  File "/Users/rob/projects/typed-parsy/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/main.py", line 100, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/main.py", line 182, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/build.py", line 191, in build
    result = _build(
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/build.py", line 265, in _build
    graph = dispatch(sources, manager, stdout)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/build.py", line 2943, in dispatch
    process_graph(graph, manager)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/build.py", line 3341, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/build.py", line 3442, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/build.py", line 2311, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/checker.py", line 481, in check_first_pass
    self.accept(d)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/checker.py", line 591, in accept
    stmt.accept(self)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/nodes.py", line 584, in accept
    return visitor.visit_overloaded_func_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/checker.py", line 628, in visit_overloaded_func_def
    self._visit_overloaded_func_def(defn)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/checker.py", line 658, in _visit_overloaded_func_def
    self.check_overlapping_overloads(defn)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/checker.py", line 768, in check_overlapping_overloads
    if is_unsafe_overlapping_overload_signatures(sig1, sig2, type_vars):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/checker.py", line 7712, in is_unsafe_overlapping_overload_signatures
    ) or is_callable_compatible(
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/subtypes.py", line 1506, in is_callable_compatible
    unified = unify_generic_callable(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/subtypes.py", line 1827, in unify_generic_callable
    cs = mypy.constraints.infer_constraints(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 314, in infer_constraints
    return _infer_constraints(template, actual, direction, skip_neg_op)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 407, in _infer_constraints
    return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/types.py", line 1964, in accept
    return visitor.visit_callable_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 1071, in visit_callable_type
    infer_callable_arguments_constraints(template, cactual, self.direction)
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 1579, in infer_callable_arguments_constraints
    res.extend(infer_directed_arg_constraints(left_arg.typ, right_arg.typ, direction))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 1539, in infer_directed_arg_constraints
    return infer_constraints(left, right, neg_op(direction))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 311, in infer_constraints
    res = _infer_constraints(template, actual, direction, skip_neg_op)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 407, in _infer_constraints
    return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/types.py", line 1429, in accept
    return visitor.visit_instance(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rob/projects/typed-parsy/.venv/lib/python3.11/site-packages/mypy/constraints.py", line 958, in visit_instance
    and unpacked.type.fullname == "builtins.tuple"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 

To Reproduce

mypy playground: https://mypy-play.net/?mypy=master&python=3.11&gist=d8a94004f71a87a6a8621da0aff744c6

This code causes the crash:

from typing import Any, Tuple, TypeVar, TypeVarTuple, Unpack, overload

T = TypeVar("T")
Ts = TypeVarTuple("Ts")


@overload
def add(self: Tuple[Unpack[Ts]], other: Tuple[T]) -> Tuple[Unpack[Ts], T]:
    ...
@overload
def add(self: Tuple[T, ...], other: Tuple[T, ...]) -> Tuple[T, ...]:
    ...
def add(self: Any, other: Any) -> Any:
    ...

Your Environment

  • Mypy version used: master (a1864d4)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.4
  • Operating system and version: macOS Ventura 13.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions