ABC with abstractmethod: kwargs on Base, explicit names on implementation

Samuel Marks samuelmarks at gmail.com
Mon Aug 24 04:24:23 EDT 2020


After a discussion on #python on Freenode, I'm here.

The gist of it is:
> Falc - Signature of method 'Pharm.foo()' does not match signature of base method in class 'Base'

What's the right way of specialising the children and leaving the Base
pretty much empty?

Specifically I want:
• All implementers to be forced to implement Base's method
• Base to be able to do very basic things with kwargs, namely log it
(to a file like stdout or a db)
• Support for [at least] Python 3.6–3.9 (I don't think `Protocol` has
been backported?)
• Implementors to have access to the proper name, rather than having
to unpack kwargs

Should I be using a Base class? - Metaclass? - Something else
entirely? - I know giving `b` a default value would resolve the
[PyCharm] linter error… but I want it to be a required argument.

Full example:

from abc import ABC, abstractmethod


class Base(ABC):
    @abstractmethod
    def foo(self, a, **kwargs):
        """
        :param a: var
        :type a: ```int```

        :param **kwargs: keyword args
        :type **kwargs: ``dict``
        """
        print(a)


class Pharm(Base):
    def foo(self, a, b):
        """
        :param a: var
        :type a: ```int```

        :param b: var
        :type b: ```int```
        """
        super(Pharm, self).foo(a=a)

Thanks,

Samuel Marks
Charity <https://sydneyscientific.org> | consultancy
<https://offscale.io> | open-source <https://github.com/offscale> |
LinkedIn <https://linkedin.com/in/samuelmarks>


More information about the Python-list mailing list