Can I have kwargs in abstract base class, but specific parameters in subclasses?

Viewed 241

Python 3.7+: I'm trying to define an ABC with an abstract method:

from abc import ABC, abstractmethod

class A(ABC):
    @abstractmethod
    def f(self, x, **kwargs):
        pass

I would like to define, in subclasses, the specific parameters each subclass supports explicitly:

class B(A):
    def f(self, x, y):
        print(x, y)

B().f("hello", "world") seems to work, but PyCharm complains with Signature of method 'B.f()' does not match signature of the base method in class 'A' and I haven't dared to test it with flake or mypy.

Am I abusing the language? Is there a better way to do it, apart from passing **kwargs unchanged? I wanted something defined in a more fixed, explicit way, like standard parameters and :param: in documentation, instead of describing to the users the parameters as strings and looking them up through string from **kwargs.

0 Answers
Related