python / python/cpython

Static abstract method link is not enforced while calling a class

未关闭
#101,605 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现所提供的 Python 3.9 示例,并检查类方法和静态方法调用所涉及的 abc 与 abstract-method 行为。将观察到的行为与 issue 中预期的两个失败案例进行比较;完成标准是实现预期的强制机制,并包含覆盖 Parent 和 Child 的回归测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。