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 due to decorator and generator interaction #18780

Closed
semohr opened this issue Mar 10, 2025 · 2 comments · Fixed by #18781
Closed

Crash due to decorator and generator interaction #18780

semohr opened this issue Mar 10, 2025 · 2 comments · Fixed by #18781
Labels
crash topic-pep-646 PEP 646 (TypeVarTuple, Unpack)

Comments

@semohr
Copy link

semohr commented Mar 10, 2025

Crash Report

This crash likely arises due to a combination of decorators and generators. I created a minimal example below (it is derived from a more complex project and might make no sense in the shown context but the crash still occurs).

Traceback

Traceback (most recent call last):
  File "mypy/checker.py", line 576, in accept
  File "mypy/nodes.py", line 923, in accept
  File "mypy/checker.py", line 5136, in visit_decorator
  File "mypy/checker.py", line 5163, in visit_decorator_inner
  File "mypy/checkexpr.py", line 1572, in check_call
  File "mypy/checkexpr.py", line 1757, in check_callable_call
  File "mypy/checkexpr.py", line 2100, in infer_function_type_arguments
  File "mypy/checkexpr.py", line 2235, in infer_function_type_arguments_pass2
  File "mypy/infer.py", line 57, in infer_function_type_arguments
  File "mypy/constraints.py", line 251, in infer_constraints_for_callable
  File "mypy/constraints.py", line 319, in infer_constraints
  File "mypy/constraints.py", line 427, in _infer_constraints
  File "mypy/types.py", line 2009, in accept
  File "mypy/constraints.py", line 1182, in visit_callable_type
  File "mypy/constraints.py", line 1349, in infer_against_any

To Reproduce

# example.py
from enum import Enum
from typing import Callable, Generator, TypeVar, TypeVarTuple

Arg = TypeVarTuple("Arg")
Ret = TypeVar("Ret")
task = TypeVar("task")


def dec1(e: str):
    def decorator(func: Callable[[*Arg], Ret]) -> Callable[[*Arg], Ret]:

        def fn(*args: *Arg) -> Ret:
            return func(*args)

        return fn

    return decorator


def to_coro(func: Callable[[*Arg, task], Ret | task]):
    def coro(*args: *Arg) -> Generator[Ret | None | task, task, None]:
        t: None | task | Ret = None
        while True:
            t = yield t
            t = func(*(args + (t,)))

    return coro


@to_coro
@dec1("asd")
def example_function(a: int, b: int) -> int:
    return a + b
mypy ./exampe.py

If I remove the dec1 the crash disappears.

My Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.11

I reproduced this with mypy version 1.14

@semohr semohr added the crash label Mar 10, 2025
@ilevkivskyi ilevkivskyi added the topic-pep-646 PEP 646 (TypeVarTuple, Unpack) label Mar 10, 2025
@ilevkivskyi
Copy link
Member

I have a fix ready, will make a PR soon. Couple comments in the meantime:

  • Your decorators are missing return types, in mypy a missing function annotation (whether return or argument) always means Any. This means those decorators "destroy" the types of functions they decorate (we have options to warn about such cases, like --disallow-untyped-decorators, or even stricter one --disallow-any-decorated).
  • Although not technically an error, usually a union of two plain type variables is a "bad" type, see if you can refactor your real code to avoid this.

JukkaL pushed a commit that referenced this issue Mar 11, 2025
Fixes #18780

Fix is trivial: handle a missing case. Note I re-use
`flatten_nested_tuples()` out of laziness. In theory, there should be at
most one level of nesting at this point, after which we should put an
assert (and IIRC we do something like this in other places). But I think
it is not worth the effort here, as this is a quite niche edge case
anyway.
@semohr
Copy link
Author

semohr commented Mar 11, 2025

Thanks for the fast fix!
I will have a look to incorporate --disallow-untyped-decorators. I think for the my real code this should already be properly implemented.

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