Skip to content

[mypy 1.6 regression] Crash involving ParamSpec, TypeAlias and overloads #16245

Closed
@AlexWaygood

Description

@AlexWaygood

Running mypy on this code causes it to crash, and exit without a traceback or error message, if you're using mypy 1.6:

from typing import Callable, Protocol, TypeVar, overload
from typing_extensions import ParamSpec, TypeAlias

_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
_Handler: TypeAlias = Callable[_P, _R_co]

class _HandlerDecorator(Protocol):
    def __call__(self, handler: _Handler[_P, _R_co]) -> _Handler[_P, _R_co]: ...

@overload
def event(event_handler: _Handler[_P, _R_co]) -> _Handler[_P, _R_co]: ...
@overload
def event(namespace: str, *args, **kwargs) -> _HandlerDecorator: ...

If you use mypy master, it prints a verrrry long traceback to the terminal indicating a RecursionError. Here's a small excerpt from the end of the traceback:

  File "C:\Users\alexw\coding\mypy\mypy\types.py", line 756, in accept
    return visitor.visit_param_spec(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\expandtype.py", line 252, in visit_param_spec
    self.expand_types(t.prefix.arg_types + repl.arg_types),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\expandtype.py", line 458, in expand_types
    a.append(t.accept(self))
             ^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\types.py", line 398, in accept
    return visitor.visit_type_alias_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\expandtype.py", line 451, in visit_type_alias_type
    args = self.expand_types_with_unpack(t.args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\expandtype.py", line 393, in expand_types_with_unpack
    items.append(item.accept(self))
                 ^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\types.py", line 756, in accept
    return visitor.visit_param_spec(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\expandtype.py", line 239, in visit_param_spec
    repl = self.variables.get(t.id, t.copy_modified(prefix=Parameters([], [], [])))
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\types.py", line 743, in copy_modified
    return ParamSpecType(
           ^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\mypy\mypy\types.py", line 719, in __init__
    super().__init__(name, fullname, id, upper_bound, default, line=line, column=column)
  File "C:\Users\alexw\coding\mypy\mypy\types.py", line 554, in __init__
    super().__init__(line, column)
RecursionError: maximum recursion depth exceeded

On mypy 1.5.1, mypy had this output when checking this code:

crasher.py:11: error: An overloaded function outside a stub file must have an implementation  [no-overload-impl]
crasher.py:12: error: Overloaded function signatures 1 and 2 overlap with incompatible return types  [misc]
Found 2 errors in 1 file (checked 1 source file)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions