python / python/mypy

The implementation of the abstract class is judged by mypy to be unconstructable

Aperta
#18,830 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-join-v-union
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Consider the following code:

#!/usr/bin/env python3

from abc import ABC, abstractmethod
from typing import override


class Base(ABC):
    def __init__(self, val: int):
        self.__val = val

    def show(self) -> None:
        print(f'hello: {self.__val}')

    @abstractmethod
    def impl(self) -> None:
        pass


class Derive1(Base):
    def __init__(self):
        super().__init__(1)

    @override
    def impl(self) -> None:
        pass


class Derive2(Base):
    def __init__(self):
        super().__init__(2)

    @override
    def impl(self) -> None:
        pass


def demo(index: int) -> Base:
    classes = [Derive1, Derive2]
    return classes[index]()


if __name__ == '__main__':
    demo(0).show()

It implements Derive1 and Derive2 on the Base abstract class, which implement the impl interface. When checking it with mypy, everything looks fine:

> mypy demo.py
Success: no issues found in 1 source file

At this time, if you try to add Derive3, the code is as follows:

#!/usr/bin/env python3

from abc import ABC, abstractmethod
from typing import override


class Base(ABC):
    def __init__(self, val: int):
        self.__val = val

    def show(self) -> None:
        print(f'hello: {self.__val}')

    @abstractmethod
    def impl(self) -> None:
        pass


class Derive1(Base):
    def __init__(self):
        super().__init__(1)

    @override
    def impl(self) -> None:
        pass


class Derive2(Base):
    def __init__(self):
        super().__init__(2)

    @override
    def impl(self) -> None:
        pass


class Derive3(Base):  # add Derive3 just like the previous two
    def __init__(self):
        super().__init__(3)

    @override
    def impl(self) -> None:
        pass


def demo(index: int) -> Base:
    classes = [Derive1, Derive2, Derive3]  # add Derive3 item
    return classes[index]()


if __name__ == '__main__':
    demo(0).show()

At this time, mypy will issue the following complaint:

> mypy demo.py
demo.py:48: error: Cannot instantiate abstract class "Base" with abstract attribute "impl"  [abstract]
Found 1 error in 1 file (checked 1 source file)

The strange thing is why there is no error when there are two implementations, but there will be an error when there are three or more.

I tried pyright and pycharm, and they both gave no error messages for the above two codes, and I didn't find any reports or explanations of the problem on the search engine.

Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: mypy demo.py
  • Python version used: 3.13.2

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo gli esempi in demo.py con mypy 1.15.0 su Python 3.13.2, confrontando le liste di due e tre classi. Analizza perché la raccolta di classi inferita produca un errore di istanziazione di Base astratta solo nel secondo caso. Il lavoro è completato quando il comportamento è spiegato e, se viene confermato che si tratta di un bug, un test di regressione impedisce la diagnosi spuria senza nascondere errori genuini relativi a classi astratte.

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
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.