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 when using ParamSpec #17825

Open
kvdveer opened this issue Sep 25, 2024 · 3 comments
Open

Crash when using ParamSpec #17825

kvdveer opened this issue Sep 25, 2024 · 3 comments
Labels
crash topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@kvdveer
Copy link

kvdveer commented Sep 25, 2024

Crash Report

I'm developing an cache decorator that uses ParamSpec to be as generic as possible.
There's a slight bug in my code (I used Sequence where I should have used Paramspec.args), but I feel that shouldn't have led to a crash. There might actually be legitimate usecases for the incorrect code I wrote.

Traceback

mypy_bug_test.py:22: 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.2
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 5822, in accept
  File "mypy/checkexpr.py", line 480, in visit_call_expr
  File "mypy/checkexpr.py", line 614, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 1467, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 1597, in check_call
  File "mypy/checkexpr.py", line 1561, in check_call
  File "mypy/checkexpr.py", line 1746, in check_callable_call
  File "mypy/checkexpr.py", line 2062, in infer_function_type_arguments
  File "mypy/checkexpr.py", line 2268, in get_arg_infer_passes
  File "mypy/subtypes.py", line 1248, in find_member
  File "mypy/subtypes.py", line 1383, in find_node_type
  File "mypy/expandtype.py", line 114, in expand_type_by_instance
  File "mypy/expandtype.py", line 69, in expand_type
  File "mypy/types.py", line 2004, in accept
  File "mypy/expandtype.py", line 408, in visit_callable_type
  File "mypy/expandtype.py", line 500, in expand_types
  File "mypy/types.py", line 769, in accept
  File "mypy/expandtype.py", line 264, in visit_param_spec
AssertionError: 
mypy_bug_test.py:22: : note: use --pdb to drop into pdb

To Reproduce

This is how far I've managed to whitle my testcase down:

from collections.abc import Callable, Sequence
import functools
from typing import Generic, ParamSpec, TypeVar

P = ParamSpec("P")
R = TypeVar("R")

class DecoratorImplementation(Generic[P, R]):
    def __init__(self,
        fn: Callable[P, Sequence[R]], param):
        self.fn = fn
        self.param = param

    def __call__(self,  *args: Sequence, **kwargs: P.kwargs):
        def wrapper(*args, **kwargs):
            return self.fn(*args, **kwargs)
        return wrapper

def decorator(func, param):

    def decorator_inner(fn):
        return functools.wraps(fn)(DecoratorImplementation(fn, param))

    return decorator_inner

Your Environment

Operating system: Debian Bookworm, python 3.11, virtualenv

mypy versions tested:

  • mypy 1.11.2 (compiled: yes) (affected) 🔴
  • mypy 1.10.0 (compiled: yes) (affected) 🔴
  • mypy 1.9.0 (compiled: yest) (NOT affected) 🟢

pyproject.toml:

[tool.mypy]
python_version = "3.11"
ignore_missing_imports = true
show_error_codes = true
check_untyped_defs = true
exclude = "tests/.*\\.py"

[[tool.mypy.overrides]]
module = "internal.config_py.*"
# lots of dict-heavy code which isn't typed at all.
ignore_errors = true
@kvdveer kvdveer added the crash label Sep 25, 2024
@brianschubert
Copy link
Collaborator

Reproduces on current master (58825f7), requires --check-untyped-defs.

Bisected to a00fcba from #16942

@JelleZijlstra
Copy link
Member

The culprit found by the bisect just changed the definition of functools.wraps. It might be worth inlining the current definition of functools.wraps into the example and bisecting again to isolate the change in mypy itself that introduced the crash.

@brianschubert
Copy link
Collaborator

brianschubert commented Sep 25, 2024

Good point! Using an inlined definition for functools.wraps,

  • First crash at 07d8878 (#11847), raises RuntimeError: Parameters cannot be constrained to at constraints.py:410
  • Change at 44993e6 (#12548), raises AssertionError: Should not be able to get here. at expandtype.py:118
  • Current crash at 14418bc (#15837), raises AssertionError at expandtype.py:246

@ilevkivskyi ilevkivskyi added the topic-paramspec PEP 612, ParamSpec, Concatenate label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

No branches or pull requests

4 participants