Cannot determine type of "__gt__" error on decorated __lt__ method where decorator is an assigned alias of another decorator
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
Okay, this one has me stumped.
I am getting a Cannot determine type of "__gt__" error on my definition of __lt__. Yes. You read that right. The error complains of __gt__, but happens on def __lt__ …. If I swap the declaration order (such that def __gt__ … appears before def __lt__ …), then I get the error Cannot determine type of "__lt__" error on my definition of __gt__, so at least it's…symmetrical.
The problem only emerges when I use an alias for a decorator created by assignment.
To Reproduce
# test_cast.py
from operator import __gt__, __lt__
from typing import Callable, TypeVar, Union
_T = TypeVar("_T")
def identity(__: _T) -> _T:
return __
maybe_not_available_decorator = identity # <-- problematic decorator alias
# try:
# from spicy import spicy_decorator as maybe_not_available_decorator
# except ImportError:
# pass
class Spam:
def __init__(self, value: int):
super().__init__()
self._value = value
@identity # <- function
def __lt__(self, other: Union[int, "Spam"]) -> bool: # <-- this is fine
if isinstance(other, Spam):
return __lt__(self._value, other._value)
else:
return NotImplemented
@identity # <- function
def __gt__(self, other: Union[int, "Spam"]) -> bool: # <-- so is this
if isinstance(other, Spam):
return __gt__(self._value, other._value)
else:
return NotImplemented
class Ham:
def __init__(self, value: int):
super().__init__()
self._value = value
@maybe_not_available_decorator # <- alias
def __lt__(self, other: Union[int, "Ham"]) -> bool: # <-- Cannot determine type of "__gt__"
if isinstance(other, Ham):
return __lt__(self._value, other._value)
else:
return NotImplemented
@maybe_not_available_decorator # <- alias
def __gt__(self, other: Union[int, "Ham"]) -> bool: # <-- this is fine for some reason
if isinstance(other, Ham):
return __gt__(self._value, other._value)
else:
return NotImplemented
Your Environment
% mypy --config-file=/dev/null test_case.py
/dev/null: No [mypy] section in config file
test_case.py:42: error: Cannot determine type of "__gt__"
Found 1 error in 1 file (checked 1 source file)
% mypy --version
mypy 0.910
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 in test_case.py ed esegui mypy --config-file=/dev/null per confermare l'errore segnalato per i metodi Ham decorati. Confronta il decoratore identity con il suo alias assegnato, quindi segui l'analisi di mypy dei decoratori e dei metodi speciali. Il lavoro è completo quando la forma con alias non produce alcun errore spurio Cannot determine type e il comportamento esistente rimane intatto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100