Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Crash due to decorator and generator interaction #18780

Description

@semohr

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions