-1
class Parent():
    def foo(self, arg1):
        ...

class Child(Parent):
    def foo(self, arg1, arg2):
        ...

Linters (I'm using Pylint) complain about this piece of code because Child().foo has different arguments than Parent().foo

What's the correct method of doing this kind of thing?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
TNTzx
  • 407
  • 2
  • 11
  • 1
    How is this method supposed to be called? – khelwood May 16 '22 at 08:22
  • 3
    The linters are complaining because this is considered bad practice, it breaks the Liskov Substitution Principle, that being said, you *can* do it and ignore your linter, but maybe you should stop and think about your overall design – juanpa.arrivillaga May 16 '22 at 08:23
  • You don't, generally, because that doesn't follow the [Liskov Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle). – jonrsharpe May 16 '22 at 08:23
  • @khelwood the method is supposed to be called on an instance, if it was a class method there would be a decorator – TNTzx May 16 '22 at 08:24
  • @juanpa.arrivillaga @jonrsharpe is it possible to add `*args` on both the `Parent` and `Child`'s `foo` methods? – TNTzx May 16 '22 at 08:24
  • 3
    @TNTzx ... right, but with how many arguments? If you have to know if it's Child or Parent so that you know the right number of arguments to pass, then it's not the same method, and it doesn't make sense to give it the same name as if it is an override. – khelwood May 16 '22 at 08:25
  • @TNTzx you can do *whatever you want*. You are free to ignore your linter. I'm not sure what you are asking – juanpa.arrivillaga May 16 '22 at 08:26
  • Related: https://stackoverflow.com/questions/45523156/inheritance-change-signature-of-child-methods and https://stackoverflow.com/questions/6034662/python-method-overriding-does-signature-matter – Aran-Fey May 16 '22 at 08:44

1 Answers1

1

The correct way according to most would be to not alter the signature at all. When you do it makes it harder to read and confusing if you need to make calls to super methods.

If you can't give the method a different name, then your next best option that would likely stop your linter from complaining is to have *args as the signature to both parent and child methods, and adjust the logic accordingly.

class Parent:
   def foo(self, *args):
       ... do something

class Child(Parent):
    def foo(self, *args):
        arg1, arg2 = args
        ... do something
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • One can indicate that the arguments are unused by naming them `*_` (or perhaps more explicitly with `*unused`) in the parent definition of the method. Argument *names* do not have to match when a method is overridden in a subclasses. – martineau May 16 '22 at 09:23
  • This papers over the issue, you might as well just use a linter directive to actually ignore it. – juanpa.arrivillaga May 16 '22 at 09:35
  • @juanpa.arrivillaga If you tell the linter to ignore it then it won't detect any unintended occurrences of the same issue located elsewhere in the project. – Alexander May 16 '22 at 10:15
  • @alexpdev pylint, and I imagine other linters, lets you ignore things on a line-by-line basis. – juanpa.arrivillaga May 16 '22 at 10:17