我如何告诉 PyLint“它是变量,而不是常量”?停止消息 C0103?
我的 Python 2.6 程序中有一个名为“_log”的模块级变量,PyLint 抱怨该变量:
C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
已阅读 这个答案 我明白它为什么这样做:它认为变量是常量并应用常量正则表达式。然而,我不敢苟同:我认为这是一个变量。我如何告诉 PyLint,这样它就不会抱怨? PyLint 如何确定它是变量还是常量 - 它只是将所有模块级变量视为常量吗?
I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:
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? How does PyLint determine whether it's a variable or a constant - does it just treat all module-level variables as constants?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将其放在您希望忽略这些警告的范围内。您还可以将上面的内容作为行尾注释,以仅针对该行代码禁用该消息。
IIRC 确实 pylint 将所有模块级变量解释为“常量”。
新版本的 pylint 将采用此行
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'.
newer versions of pylint will take this line instead
您还可以指定 pylintrc 中始终允许的以逗号分隔的“好名称”列表,例如:
You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg:
在我看来,一些重构可能会有所帮助。 Pylint 将其视为一个模块,因此不期望在这个级别看到变量是合理的。相反,它不会抱怨类或函数中的变量。以下范例似乎很常见并解决了问题:
这样做的好处是,如果您有一些有用的类,我可以导入它们而无需运行您的 main.c 文件。 __name__ 是模块的名称,因此“if”失败。
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. Conversely it doesn't complain about vars in classes or functions. The following paradigm seems quite common and solves the issue:
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.
在较新版本的 pylint 中,此行现在
启用消息非常简单
In newer versions of pylint this line is now
the enable message is as simple
正如其他答案所示,您可以通过包含以下行来禁用特定的 PyLint 警告(例如 C0103):
但这会生成
Locally disabling invalid-name
警告。请注意,如果您想提醒您已禁用的警告,则此辅助警告可能会很有用。如果您想在不更改配置文件的情况下静默禁用警告(这将全局禁用警告),您可以使用:请注意,PyLint 不会发出您正在禁用 I0011 的警告!
As other answers have indicated you can disable a specific PyLint warning (such C0103) as by including the following line:
but this generates the
Locally disabling invalid-name
warning. 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:Note that PyLint does not issue a warning that you are disabling I0011!
如果您在文件中本地禁用消息,那么 Pylint 将报告另一个不同的警告!
如果您的目的是进行干净的 lint 运行,并且这肯定应该是目标,否则您为什么要烦恼,那么您可以在配置文件中禁用该消息和相应的本地启用消息:
If you disable a message locally in your file then Pylint will report another different warning!
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: