stam
stam

Reputation: 339

decrease mandatory parameters (change method signature) on overriden method best practices (i.e. to avoid pylint errors)

I have a generic parent class with mymethod and specific child class, where some parameters required for mymethod are known - user of child don't even need to know about them. I want to override mymethod so it only require param1, however pylint complains about inherited method changed signature - W0221: Number of parameters was *** in *** and is now *** in overridden *** method (arguments-differ)

What's the best practice for this use case? I've tried adding **kwargs to either or both methods, but no help (even if it would I'd then have to work around 'unused arguments' issue?) I could create ChildA.mymethod2 with only accepting param1 and within it call super().mymethod(param1, param2, param3) filling remaining parameters, but that seems rather awkward - is there better way to achieve this?

class Parent():

    def mymethod(self, param1, param2, param3):  
        log.info(f'func a with {param1} and {param2} and {param3}')

class ChildA(Parent):

    def mymethod(self, param1, **kwargs):  # kwargs added hoping to 'fool' pylint about signature arguments, no help
        param2 = 2
        param3 = 3
        super().func(param1, param2, param3)

desired state is having Parent and numerous Child classes (each defining its own param2 and param3) passing pylint, while user can only do

c = ChildA()
c.mymethod(param1)

Upvotes: 0

Views: 232

Answers (1)

A.J.X
A.J.X

Reputation: 55

You could try using default arguments in the child classes:

class Parent():
    def mymethod(self, param1, param2, param3):  
        log.info(f'func a with {param1} and {param2} and {param3}')

class ChildA(Parent):
    def mymethod(self, param1, param2=None, param3=None):
        param2 = 2
        param3 = 3
        super().func(param1, param2, param3)

Upvotes: 2

Related Questions