abstract staticmethod throws TypeError when used by subclass instances

214 Views Asked by At

An ABC superclass defines several abstract methods and an abstract static method for which all 3 methods need to be implemented in the subclasses:

class Tool(abc.ABC):

    @abc.abstractmethod
    def do_this(self) -> None: pass

    @abc.abstractmethod
    def do_that(self) -> None: pass

    @staticmethod
    @abc.abstractmethod
    def name() -> str: pass

class MyTool(Tool):

    def do_this(self) -> None: # do this

    def do_that(self) -> None: # do that

    def name() -> str: return 'MyTool'

>>> MyTool.name()
'MyTool'
>>> MyTool().name()
TypeError: name() takes 0 positional arguments but 1 was given

This does not persist if the method name in the subclass is decorated with @staticmethod.

What is taking place here? Why does this only happen when the superclass is a type of abc.ABC?

Using Python 3.6 so use of abc.staticmethod is depreciated, but still yields same result.

0

There are 0 best solutions below