Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylint false positive for unused argument

Tags:

python

pylint

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?

like image 596
Rudy Bunel Avatar asked Jan 02 '14 11:01

Rudy Bunel


3 Answers

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

like image 97
Holy Mackerel Avatar answered Sep 28 '22 04:09

Holy Mackerel


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.

like image 34
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 04:09

Ignacio Vazquez-Abrams


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.

like image 36
isilanes Avatar answered Sep 28 '22 05:09

isilanes