使用 pylint 的 python 代码约定

发布于 2024-07-16 10:05:50 字数 328 浏览 4 评论 0原文

我正在尝试 pylint 来检查我的源代码的约定。 不知何故,某些变量名称与常量的正则表达式 (const-rgx) 匹配,而不是与变量名称正则表达式 (variable-rgx) 匹配。 如何将变量名与variable-rgx匹配? 或者我应该用我的 variable-rgx 东西扩展 const-rgx 吗?

例如
C0103:31:名称“settings”无效(应匹配 (([A-Z_][A-Z1-9_]*)|(__.*__))$)

I'm trying out pylint to check my source code for conventions. Somehow some variable names are matched with the regex for constants (const-rgx) instead of the variable name regex (variable-rgx). How to match the variable name with variable-rgx? Or should I extend const-rgx with my variable-rgx stuff?

e.g.
C0103: 31: Invalid name "settings" (should match (([A-Z_][A-Z1-9_]*)|(__.*__))$)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一身软味 2024-07-23 10:05:50

不知何故,某些变量名称与常量的正则表达式(const-rgx)匹配,而不是与变量名称正则表达式(variable-rgx)匹配。

这些变量是在模块级别声明的吗? 也许这就是为什么它们被视为常量(至少根据 PEP-8,它们应该是这样声明的)。

Somehow some variable names are matched with the regex for constants (const-rgx) instead of the variable name regex (variable-rgx).

Are those variables declared on module level? Maybe that's why they are treated as constants (at least that's how they should be declared, according to PEP-8).

萌化 2024-07-23 10:05:50

我只是禁用该警告,因为我不遵循这些命名约定。

为此,请将此行添加到模块的顶部:

# pylint: disable-msg=C0103

如果要全局禁用它,请将其添加到 pylint 命令中:

python lint.py --disable-msg=C0103 ...

I just disable that warning because I don't follow those naming conventions.

To do that, add this line to the top of you module:

# pylint: disable-msg=C0103

If you want to disable that globally, then add it to the pylint command:

python lint.py --disable-msg=C0103 ...
冬天旳寂寞 2024-07-23 10:05:50

(should match (([A-Z_][A-Z1-9_]*)|(__.*__))$)

就像你说的那样, const-rgx 只匹配大写名称或双下划线包围的名称。

变量-rgx 是

([a-z_][a-z0-9_]{2,30}$)

如果你的变量被称为“设置”,它确实应该与变量-rgx 相匹配,

我只能想到两个原因。
设置要么是常量,要么是 PyLint 中的错误。


(should match (([A-Z_][A-Z1-9_]*)|(__.*__))$)

like you said that is the const-rgx that is only matching UPPERCASE names, or names surrounded by double underscores.

the variables-rgx is

([a-z_][a-z0-9_]{2,30}$)

if your variable is called 'settings' that indeed should match the variables-rgx

I can think of only 2 reasons for this..
either settings is a constant or it is a bug in PyLint.

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