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? How does PyLint determine whether it's a variable or a constant - does it just treat all module-level variables as constants?
读完这个答案后,我理解为什么会这样做:它认为变量是一个常量并应用常量正则表达式。但是,我不同意:我认为这是一个变数。我怎么告诉PyLint,所以它没有抱怨? PyLint如何确定它是变量还是常量 - 它是否只将所有模块级变量视为常量?
6 个解决方案
#1
79
# 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
#2
15
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:
在我看来,一些重构可能会有所帮助。 Pylint将此视为一个模块,因此不期望在此级别看到变量是合理的。相反,它不会抱怨类或函数中的变量。以下范例似乎很常见并解决了这个问题:
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”失败。
#3
15
You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg:
您还可以指定逗号分隔的“好名字”列表,这些列表在您的pylintrc中始终允许,例如:
[BASIC]
good-names=_log
#4
11
In newer versions of pylint this line is now
在较新版本的pylint中,现在是这一行
# pylint: disable=C0103
the enable message is as simple
启用消息很简单
# pylint: enable=C0103
#5
2
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
#6
2
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. 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的警告!
#1
79
# 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
#2
15
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:
在我看来,一些重构可能会有所帮助。 Pylint将此视为一个模块,因此不期望在此级别看到变量是合理的。相反,它不会抱怨类或函数中的变量。以下范例似乎很常见并解决了这个问题:
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”失败。
#3
15
You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg:
您还可以指定逗号分隔的“好名字”列表,这些列表在您的pylintrc中始终允许,例如:
[BASIC]
good-names=_log
#4
11
In newer versions of pylint this line is now
在较新版本的pylint中,现在是这一行
# pylint: disable=C0103
the enable message is as simple
启用消息很简单
# pylint: enable=C0103
#5
2
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
#6
2
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. 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的警告!