__new__ on abstract classes produces false positive "Cannot instantiate abstract class"
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Descrizione
Bug Report
When an abstract class defines __new__ (note, not __init__) method, mypy produces error when that class is instantiated, even though __new__() does not imply instantiation.
To Reproduce
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
def __new__(cls) -> "Abstract":
return super().__new__(Concrete)
@abstractmethod
def hello(self) -> int:
...
class Concrete(Abstract):
def hello(self) -> int:
return 0
x = Abstract()
print(x.hello())
Expected Behavior
No errors reported when checked with mypy. Calling a class with __new__ method shuld be effectively treated as just calling a simple method, like
def make_abstract() -> "Abstract":
return Concrete()
Actual Behavior
error message is printed:
error: Cannot instantiate abstract class "Abstract" with abstract attribute "hello" [abstract]
Your Environment
- Mypy version used: mypy-0.991
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10.6
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
Nell’issue non sono indicati file del repository né test. Inizia dal riproduttore Python fornito e segui il controllo di mypy dell’istanziazione di classi astratte quando è definito new; il lavoro è completato quando il falso errore [abstract] non è più presente e la diagnosi prevista per le classi astratte continua a essere coperta da un test di regressione.
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