如何禁用文件中特定变量的pylint“未定义变量"错误? [英] How to disable pylint 'Undefined variable' error for a specific variable in a file?

查看:53
本文介绍了如何禁用文件中特定变量的pylint“未定义变量"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 应用程序中托管 IronPython,并将主机的 API 注入全局范围.

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

我刚刚开始喜欢 syntastic 用于 vim 和 pylint 来检查我的脚本.但是我对注入变量的所有 [E0602, method_name] Undefined variable 'variable_name' 错误消息感到恼火.

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.

我知道使用 # pylint: disable=E0602 来禁用此错误消息,但我不希望仅针对某些特定变量名称削弱一个非常有用的功能.

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!!

我真正想做的是:

# pylint: declare=host_object, other_stuff

推荐答案

在代码中禁用 E0602:

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

显然,这需要每个模块执行一次,在这一行之后出现的所有 injected_var 对于 pylint 都是合法的.

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

这篇关于如何禁用文件中特定变量的pylint“未定义变量"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆