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

发布于 2024-08-13 19:46:07 字数 391 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧城烟雨 2024-08-20 19:46:07
# pylint: disable-msg=C0103

将其放在您希望忽略这些警告的范围内。您还可以将上面的内容作为行尾注释,以仅针对该行代码禁用该消息。

IIRC 确实 pylint 将所有模块级变量解释为“常量”。

新版本的 pylint 将采用此行

# pylint: disable=C0103
# 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'.

newer versions of pylint will take this line instead

# pylint: disable=C0103
云之铃。 2024-08-20 19:46:07

您还可以指定 pylintrc 中始终允许的以逗号分隔的“好名称”列表,例如:

[BASIC]
good-names=_log

You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg:

[BASIC]
good-names=_log
久随 2024-08-20 19:46:07

在我看来,一些重构可能会有所帮助。 Pylint 将其视为一个模块,因此不期望在这个级别看到变量是合理的。相反,它不会抱怨类或函数中的变量。以下范例似乎很常见并解决了问题:

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

if __name__ == '__main__':
    main()

这样做的好处是,如果您有一些有用的类,我可以导入它们而无需运行您的 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:

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.

平安喜乐 2024-08-20 19:46:07

在较新版本的 pylint 中,此行现在

# pylint: disable=C0103

启用消息非常简单

# pylint: enable=C0103

In newer versions of pylint this line is now

# pylint: disable=C0103

the enable message is as simple

# pylint: enable=C0103
鹤舞 2024-08-20 19:46:07

正如其他答案所示,您可以通过包含以下行来禁用特定的 PyLint 警告(例如 C0103):

# pylint: disable=C0103

但这会生成 Locally disabling invalid-name 警告。请注意,如果您想提醒您已禁用的警告,则此辅助警告可能会很有用。如果您想在不更改配置文件的情况下静默禁用警告(这将全局禁用警告),您可以使用:

# pylint: disable=I0011,C0103

请注意,PyLint 不会发出您正在禁用 I0011 的警告!

As other answers have indicated you can disable a specific PyLint warning (such C0103) as by including the following line:

# pylint: disable=C0103

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:

# pylint: disable=I0011,C0103

Note that PyLint does not issue a warning that you are disabling I0011!

爱情眠于流年 2024-08-20 19:46:07

如果您在文件中本地禁用消息,那么 Pylint 将报告另一个不同的警告!

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

如果您的目的是进行干净的 lint 运行,并且这肯定应该是目标,否则您为什么要烦恼,那么您可以在配置文件中禁用该消息和相应的本地启用消息:

disable=locally-disabled, locally-enabled

If you disable a message locally in your file then Pylint will report another different warning!

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:

disable=locally-disabled, locally-enabled
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文