Question

I'm in the process of cleaning up code with pylint in order to be able to use it for pre-commit validation. I have a lot of "unused-argument" warning when in fact they are used. Here is an example triggering a false positive.

def addSeven(foo): #Here I have a warning "Unused argument 'foo'"
    foo += [7]

example = [3, 4, 5, 6]

addSeven(example)
print example

I do not wish to suppress globally this warning because i would like to see the times when an argument is really unused. Is there an other option that manually adding a disable comment in each occurence? Is it a known problem with pylint?

Was it helpful?

Solution 2

This is reasonable behavior from pylint; if the object passed is immutable then the statement given is essentially a no-op. Only when is mutable does it seem incorrect.

Unfortunately if you don't want to globally disable the warning then you will need to disable it per instance.

OTHER TIPS

You can disable it for any scope by adding:

def myfunc(a):
    # pylint: disable=W0612,W0613

see https://pylint.readthedocs.io/en/latest/faq.html#is-it-possible-to-locally-disable-a-particular-message

pylint is generally a good indicator of bad style. Even when it gives a "false positive", it is probably due to doing things against convention. I am no expert, but I'd say a function that only has a side effect is not optimal. Some people (Robert Martin in Clean Code, for example), go as far as saying all side effects are lies.

I'd recommend (again, I am no expert):

def addSeven(foo):
    return foo + [7]

example = [3, 4, 5, 6]

example = addSeven(example)

Arguments should be input-only, and output should be via return value. Output arguments are bad practice, as far as I know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top