方法名与snake_case命名sty不一致

2024-05-23 18:00:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pylintrc文件创建一个简单的项目,并获取测试方法的以下错误:

method name - test_calculator_add_method_returns_correct_result -  doesn't conform to snake_case naming style
class TddInPythonExample(unittest.TestCase):
    """ This is a basic test class"""

    def test_calculator_add_method_returns_correct_result(self):
        """ This test the calculator add method """
        calc = Calculator()
        result = calc.add(2,2)
        self.assertEqual(4, result)

Tags: 项目nametestselfadd错误calcresult
3条回答

如果您是想忽略此问题的Visual Studio代码用户,可以将python.linting.pylintArgs添加到.vscode/settings.json

{
    ...
    "python.linting.pylintArgs": [
        "--disable=C0103"
    ]
    ...
}

由@jrtapsell指出

要添加更多信息:

在命名约定方面,为每种类型定义了一个正则表达式。

您可能会注意到一个名称的长度可以从2到30个字符以及它的regex。

    +-------------------+---------------+-------------------------------------------+
    |       Type        |    Option     |        Default regular expression         |
    +-------------------+---------------+-------------------------------------------+
    | Argument          | argument-rgx  | [a-z_][a-z0-9_]{2,30}$                    |
    | Attribute         | attr-rgx      | [a-z_][a-z0-9_]{2,30}$                    |
    | Class             | class-rgx     | [A-Z_][a-zA-Z0-9]+$                       |
    | Constant          | const-rgx     | (([A-Z_][A-Z0-9_]*)|(__.*__))$            |
    | Function          | function-rgx  | [a-z_][a-z0-9_]{2,30}$                    |
    | Method            | method-rgx    | [a-z_][a-z0-9_]{2,30}$                    |
    | Module            | module-rgx    | (([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ |
    | Variable          | variable-rgx  | [a-z_][a-z0-9_]{2,30}$                    |
    | Variable, inline1 | inlinevar-rgx | [A-Za-z_][A-Za-z0-9_]*$                   |
    +-------------------+---------------+-------------------------------------------+

来源:http://pylint-messages.wikidot.com/messages:c0103

为什么方法名被拒绝

根据这个:http://pylint-messages.wikidot.com/messages:c0103,名称的长度限制为30个字符,其中方法名的长度为49个字符

修复

您可以缩短方法名,或更改配置以允许更长的方法

相关问题 更多 >