Rudy Bunel
Rudy Bunel

Reputation: 794

pylint false positive for unused argument

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?

Upvotes: 11

Views: 11439

Answers (3)

Holy Mackerel
Holy Mackerel

Reputation: 3279

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

Upvotes: 14

isilanes
isilanes

Reputation: 149

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.

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798486

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.

Upvotes: 5

Related Questions