python / python/cpython

Static abstract method link is not enforced while calling a class

Aperta
#101,605 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
36k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

If you define a method both static and abstract, for example in an interface like concept, you will expect that method to be implemented in all subclasses.

It works fine when calling instances of that child class, raising an exception if the static method is not implemented.
Also, IDE such as Pycharm will warn you that you are doing something fishy.

But you don't get an exception if you simply call the static method.

In the following example I would expect :

  • to get an exception while calling Child telling me that this class is not implementing some method properly, such as method1.
  • to get an exception while calling Parent static abstract method directly
from abc import ABC, abstractmethod


class Parent(ABC):
    @staticmethod
    @abstractmethod
    def method1():
        pass

    @staticmethod
    @abstractmethod
    def method2():
        print("Method 2 in Parent")

    @abstractmethod
    def method3(self):
        pass

    @abstractmethod
    def method4(self):
        print("Method 4 in Parent")


class Child(Parent):
    @staticmethod
    def method2():
        print("Method 2 in Child")

    def method4(self):
        print("Method 4 in Child")


def test_abstract_class():
    Parent.method1() # Should complain because calling an abstract class should be forbidden
    Parent.method2() # Should complain because calling an abstract class should be forbidden
    Child.method1() # Should complain because method was not defined
    Child.method2()
    print(issubclass(Child, Parent))

Your environment

Python 3.9 on windows 10

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 riproducendo l’esempio fornito in Python 3.9 e analizza il comportamento di abc e abstract-method coinvolto nelle chiamate a metodi di classe e statici. Confronta il comportamento osservato con i due casi di errore previsti nell’issue; il lavoro è completo quando l’applicazione prevista è implementata con test di regressione che coprono Parent e Child.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.