Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a method in the superclass so that it will be compatible with any subclass?

I have a few classes:

class Sup:
    def meth (self, ?????):
        pass

class Sub1(Sup):
    def meth (self, foo = 1):
        ...

class Sub2(Sup):
    def meth (self, bar = 2, baz = 3):
        ...

what do I replace ????? in Sup.meth with?

Basically, I want something similar to the ignored &rest lambda-list keyword in Lisp to pacify arguments-differ/Arguments number differs from overridden method warning.

What I tried:

  1. adding **kwargs to Sup.meth - did not change anything
  2. annotating Sub*.meth with # pylint: disable=arguments-differ - works, but ugly.

PS. Sup is an abstract class to collect stats; Sub* are different methods to do that; meth prints the object with different bells and whistles.

like image 372
sds Avatar asked Nov 20 '14 17:11

sds


1 Answers

I've found that pylint has... unusual opinions about many Python features. It thinks some_function(*list_of_args) is "bad magic", for example (W0124).

When you and pylint will never agree on a "problem", the solution isn't weird comments in your code, but silencing that message forever in ~/.pylintrc. (pylint --generate-rcfile will spit out a sample config file that you can edit yourself.) There, you can set:

  • A regex for deliberately-unused variables: dummy-variables-rgx=_$|__$

  • Regexes for variable, class, and constant names: function-rgx=[a-z_][a-z0-9_]{0,30}$

  • Silence individual warnings or whole message categories (one of "IRCWEF"): disable=I0011,W0142,R.

~/.pylintrc is the only way I've found you can disable some things --- like "I0011: "Locally disabling %s", because locally silencing I0011 itself generates another I0011 message (which is downright sadistic).

For projects, I typically create a one-line shell script to further disable things I don't intend to fix:

#!/bin/bash

pylint --output=colorized --disable=C0103 *.py | less -R -
#  :C0103 (invalid-name): *Invalid name "%s" for type %s (should match %s)*

Translation: "I already know the previous guy had his own naming convention, so please don't tell me another 8000 times."

like image 90
Kevin J. Chase Avatar answered Sep 28 '22 04:09

Kevin J. Chase