1 year ago

#389185

test-img

pstatix

abstract staticmethod throws TypeError when used by subclass instances

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.

python

class

static-methods

abc

0 Answers

Your Answer

Accepted video resources