python / python/cpython

Static abstract method link is not enforced while calling a class

Open
#101,605 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Python 3.9 example and inspect the abc and abstract-method behavior involved in class and static-method calls. Compare the observed behavior with the two expected failure cases in the issue; done means the intended enforcement is implemented with regression tests covering Parent and Child.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.