python / python/mypy

False positive in return type on a decorator factory whose results can decorate functions or coroutines

Open
#14,776 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy gives a false positive (with printed error messages indicating that matching types don't match) on the following code:

from typing import Callable, TypeVar, Coroutine, Any, Union, cast
import asyncio
import functools
import time

F = TypeVar('F', bound=Union[Callable[..., Any], Callable[..., Coroutine[Any, Any, Any]]])


def retry_forever() -> Callable[[F], F]:
    """This function returns a decorator which causes the decorated function or coroutine to
    retry repeatedly 
    :returns: A decorator
    """
    def __decorator(f: F) -> F:
        if asyncio.iscoroutinefunction(f):
            @functools.wraps(f)
            async def __inner(*args, **kwargs) -> Any:
                while True:
                    try:
                        f(*args, **kwargs)
                    except Exception:
                        await asyncio.sleep(1)
                    else:
                        break
            return __inner # error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]]", expected "F")  [return-value]
        else:
            @functools.wraps(f)
            def __inner(*args, **kwargs) -> Any:
                while True:
                    try:
                        f(*args, **kwargs)
                    except Exception:
                        time.sleep(1)
                    else:
                        break
            return __inner # error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]]", expected "F")  [return-value]
    return __decorator

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.10&gist=2819b5f48c59c1dde77fc502f34a023c

Expected Behavior
Since the error message reports types which match (modulo the mypy documentation here) this should pass type checking

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: None
  • Python version used: 3.10

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided mypy-play reproducer and inspect the decorator return-value checking path for generic callables that may wrap functions or coroutines. Add a regression test for the shown snippet, then run the relevant type-checking tests; done means the matching return types no longer produce a false positive.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.