简体   繁体   English

如何告诉PyLint“它是变量,而不是常量”来停止消息C0103?

[英]How do I tell PyLint “it's a variable, not a constant” to stop message C0103?

I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about: 我的Python 2.6程序中有一个名为“_log”的模块级变量,PyLint抱怨说:

C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)

Having read this answer I understand why it's doing this: it thinks the variable is a constant and applies the constant regex. 读完这个答案后,我理解为什么会这样做:它认为变量是常量并应用常量正则表达式。 However, I beg to differ: I think it's a variable. 但是,我不同意:我认为这是一个变数。 How do I tell PyLint that, so it doesn't complain? 我怎么告诉PyLint,所以它没有抱怨? How does PyLint determine whether it's a variable or a constant - does it just treat all module-level variables as constants? PyLint如何确定它是变量还是常量 - 它是否只将所有模块级变量视为常量?

# pylint: disable-msg=C0103

Put it in the scope where you want these warnings to be ignored. 将它放在您希望忽略这些警告的范围内。 You can also make the above an end-of-line comment, to disable the message only for that line of code. 您还可以将上述内容作为行尾注释,以仅为该行代码禁用该消息。

IIRC it is true that pylint interprets all module-level variables as being 'constants'. IIRC确实pylint将所有模块级变量解释为'常量'。

newer versions of pylint will take this line instead 较新版本的pylint将改为使用此行

# pylint: disable=C0103

You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg: 您还可以指定逗号分隔的“好名字”列表,这些列表在您的pylintrc中始终允许,例如:

[BASIC]
good-names=_log

Seems to me a bit of refactor might help. 在我看来,一些重构可能有所帮助。 Pylint in looking at this as a module, so it would be reasonable not to expect to see variables at this level. Pylint将此视为一个模块,因此不期望在此级别看到变量是合理的。 Conversely it doesn't complain about vars in classes or functions. 相反,它不会抱怨类或函数中的变量。 The following paradigm seems quite common and solves the issue: 以下范例似乎很常见并解决了这个问题:

def main():
    '''Entry point if called as an executable'''
    _log = MyLog()  # . . .

if __name__ == '__main__':
    main()

This has the benefit that if you have some useful classes, I can import them without running your main. 这样做的好处是,如果你有一些有用的类,我可以导入它们而无需运行你的主。 The __name__ is that of the module so the "if" fails. __name__是模块的,因此“if”失败。

In newer versions of pylint this line is now 在较新版本的pylint中,现在是这一行

# pylint: disable=C0103

the enable message is as simple 启用消息很简单

# pylint: enable=C0103

As other answers have indicated you can disable a specific PyLint warning (such C0103) as by including the following line: 正如其他答案所示,您可以通过包含以下行禁用特定的PyLint警告(例如C0103):

# pylint: disable=C0103

but this generates the Locally disabling invalid-name warning. 但这会生成Locally disabling invalid-name警告。 Note that this secondary warning could be useful if you want to be reminded of the disabled warning. 请注意,如果要提醒已禁用警告,则此次要警告可能很有用。 If you want to disable the warning silently without altering your config file (which would disable the warning globally) you can use: 如果要在不更改配置文件的情况下静默禁用警告(这将全局禁用警告),您可以使用:

# pylint: disable=I0011,C0103

Note that PyLint does not issue a warning that you are disabling I0011! 请注意,PyLint不会发出禁用I0011的警告!

If you disable a message locally in your file then Pylint will report another different warning! 如果您在文件中本地禁用了一条消息,那么Pylint将报告另一个不同的警告!

Locally disabling invalid-name (C0103) [I:locally-disabled] 

If your intention is for a clean lint run, and surely that should be the target otherwise why are you bothering, then you can disable that message and the corresponding locally-enabled message in your configuration file: 如果您的目的是进行干净的lint运行,并且肯定应该是目标,否则您为什么要打扰,那么您可以在配置文件中禁用该消息和相应的本地启用的消息:

disable=locally-disabled, locally-enabled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM