Mypy not detecting abstract methods returning undefined types
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
I have a class with a couple of abstract methods that return a particular custom type. That custom type is then defined right after the class. Then a global variable referencing that class is created:
To Reproduce
# test.py
from abc import ABC, abstractmethod
from typing import NamedTuple, List
class F(ABC):
@abstractmethod
async def m1(self) -> Result:
pass
@abstractmethod
async def m2(self) -> Result:
pass
class Result(NamedTuple):
f1: str
f2: int
foo: List[F] = []
> mypy --strict test.py
Success: no issues found in 1 source file
> python test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
class F(ABC):
File "test.py", line 6, in F
async def m1(self) -> Result:
NameError: name 'Result' is not defined
Expected Behavior
I would expect mypy to detect that Result is not defined before its use in class F.
Actual Behavior
Mypy is happy with the code as it is, even under --strict, while Python can't even run the code as is. Moving the declaration of Result above class F solves the problem and both Python and mypy are then happy.
Your Environment
- Mypy version used: 0.991
- Mypy command-line flags: --strict
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.9.13
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
Riproduci il comportamento di test.py eseguendo mypy --strict e poi Python per confrontare il risultato statico con il NameError a runtime. Traccia il modo in cui mypy analizza le annotazioni del valore restituito dal metodo astratto prima che Result venga dichiarato. Il lavoro è completato quando mypy segnala il riferimento a Result non definito in questo caso senza compromettere le annotazioni valide.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100