Search code examples
pythonpylint

How to make pylint count only arguments without a default value?


By default, pylint fails (returns errorcode>0) if a method has more than 5 parameters.

def my_func(a, b, c, d, e, f, g)

will therefore fail. I would like to keep this behavior but allow functions that pass default values like

def my_func2(a, b, c, d=None, e='yes', f=1.0, g=None, h=True, i=10)

not to fail.

In other words, I would like pylint to only count parameters without default value. Can this be done? How? Is there a regexp for the parameters?

Note: raising max-args does not help in this respect since my_func2 actually receives more parameters than my-func


Solution

  • After a few more days of playing around with this I found a workaround.

    It is possible to disable specific pylint checks for a specific line of a specific block of code, as described here.

    Since in my case there is a specific function for which I think it is OK to violate the too-many-arguments check, I can add as the first line after the function definition the an instruction to pylint to disable the check:

    def my_func2(a, b, c, d=None, e='yes', f=1.0, g=None, h=True, i=10):
        # pylint: disable=too-many-arguments
        pass
    

    This disables the error, and the pylint check completes successfully.

    While this is a workaround to my original question I am OK with this solution, because it requires the developer to actively say that this is an intentional violation of pylint guidelines that is acceptable.