python / python/cpython

Static abstract method link is not enforced while calling a class

オープン
#101,605 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された Python 3.9 の例を再現し、クラスメソッドおよび static-method の呼び出しに関係する abc と abstract-method の挙動を調べます。観測された挙動を issue にある 2 つの想定される失敗ケースと比較します。Parent と Child を対象とする回帰テストによって意図した強制が実装されていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。