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

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

Closed
AlexWaygood opened this issue Oct 11, 2023 · 1 comment · Fixed by #16251
Closed

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

AlexWaygood opened this issue Oct 11, 2023 · 1 comment · Fixed by #16251
Labels
affects-typeshed Anything that blocks a typeshed change crash priority-0-high topic-overloads topic-paramspec PEP 612, ParamSpec, Concatenate topic-type-alias TypeAlias and other type alias issues

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 11, 2023

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)
@AlexWaygood AlexWaygood added crash priority-0-high topic-overloads topic-paramspec PEP 612, ParamSpec, Concatenate topic-type-alias TypeAlias and other type alias issues affects-typeshed Anything that blocks a typeshed change labels Oct 11, 2023
@AlexWaygood
Copy link
Member Author

14418bc3d2c38b9ea776da6029e9d9dc6650b7ea is the first bad commit
commit 14418bc3d2c38b9ea776da6029e9d9dc6650b7ea
Author: Ivan Levkivskyi <[email protected]>
Date:   Tue Aug 15 20:31:26 2023 +0100

    Polymorphic inference: support for parameter specifications and lambdas (#15837)

Bisects to 14418bc (cc. @ilevkivskyi)

@AlexWaygood AlexWaygood changed the title Crash involving ParamSpec, TypeAlias and overloads [mypy 1.6 regression] Crash involving ParamSpec, TypeAlias and overloads Oct 11, 2023
ilevkivskyi added a commit that referenced this issue Oct 12, 2023
Fixes #16245
Fixes #16248

Unfortunately I was a bit reckless with parentheses, but in my defense
`unify_generic_callable()` is kind of broken for long time, as it can
return "solutions" like ```{1: T`1}```. We need a more principled
approach there (IIRC there is already an issue about this in the scope
of `--new-type-inference`).

(The fix is quite trivial so I am not going to wait for review too long
to save time, unless there will be some issues in `mypy_primer` etc.)
hauntsaninja pushed a commit that referenced this issue Oct 17, 2023
Fixes #16245
Fixes #16248

Unfortunately I was a bit reckless with parentheses, but in my defense
`unify_generic_callable()` is kind of broken for long time, as it can
return "solutions" like ```{1: T`1}```. We need a more principled
approach there (IIRC there is already an issue about this in the scope
of `--new-type-inference`).

(The fix is quite trivial so I am not going to wait for review too long
to save time, unless there will be some issues in `mypy_primer` etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-typeshed Anything that blocks a typeshed change crash priority-0-high topic-overloads topic-paramspec PEP 612, ParamSpec, Concatenate topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant