False positive in return type on a decorator factory whose results can decorate functions or coroutines
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore mypy-play fornito e analizza il percorso di verifica dei valori di ritorno per i callable generici che possono racchiudere funzioni o coroutine. Aggiungi un test di regressione per lo snippet mostrato, quindi esegui i test di verifica dei tipi pertinenti; il lavoro è terminato quando i tipi di ritorno corrispondenti non producono più un falso positivo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100