Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint isn't ignoring dummy variables

Tags:

python

pylint

Pylint has a configuration option which is suppose to let you do things like this:

for dummy_var in range(10):
    print 'hello world'

Ordinarily, having an unused variable like dummy_var would give you a warning, but since the beginning matches the regular expression in dummy-variables-rgx, no warning is given. By default the value is _|dummy, so the following should generate no warnings:

for _ in range(10):
    print 'hello world'

The problem is that it does. It gives the warning about an invalid name "_".

How can I fix this?

like image 771
jlund3 Avatar asked Jan 31 '26 17:01

jlund3


2 Answers

If I had read the error message more carefully, I would have realized that I was no longer getting an unused variable warning, but a warning about a bad variable name. In other words, something like this would be fine:

for _unused in range(10):
    print 'hello world'

This is okay since it matches the regular expression in variable-rgx. However, the original form

for _ in range(10):
    print 'hello world'

does not work since _ does not match the value of variable-rgx. In order to have no warnings for this unused and invalid variable name, the name needs to be added to the list of white-listed names in good-names. Once this is done, no more warnings will be generated.

like image 136
jlund3 Avatar answered Feb 02 '26 05:02

jlund3


I added this to my .pylintrc:

dummy-variables-rgx=^_.*
like image 31
kzh Avatar answered Feb 02 '26 05:02

kzh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!