Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable pylint 'Undefined variable' error for a specific variable in a file?

Tags:

python

pylint

I am hosting IronPython inside a C# application and injecting an API for the host into the global scope.

I have just started to love syntastic for vim with pylint for checking my scripts. But I am getting annoyed by all the [E0602, method_name] Undefined variable 'variable_name' error messages for the injected variables.

I am aware of using # pylint: disable=E0602 to disable this error message, but I'd prefer not to cripple a really useful feature just for some specific variable names.

How do you deal with this?

Currently, I am doing this at the top of my script:

try:
    host_object = getattr(__builtins__, 'host_object')
except AttributeError:
    pass # oops, run this script inside the host application!!

What I would really like to do is this:

# pylint: declare=host_object, other_stuff
like image 345
Daren Thomas Avatar asked Feb 26 '13 12:02

Daren Thomas


2 Answers

You can add your variables to the 'additional-builtins' option so pylint will consider them as defined.

This has to be done in a rc file, it can't be done inlined in the code.

like image 178
sthenault Avatar answered Oct 16 '22 14:10

sthenault


Disabling E0602 in the code:

# make pylint think that it knows about 'injected_var' variable
injected_var = injected_var  # pylint:disable=invalid-name,used-before-assignment

Obviously, that needs to be done once per module, all occurrences of injected_var after this line would be legal for pylint.

like image 28
void Avatar answered Oct 16 '22 14:10

void