20

我正在使用我的 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)
4

5 回答 5

33

为什么方法名被拒绝

据此显示:http ://pylint-messages.wikidot.com/messages:c0103名称的长度上限为 30 个字符,其中您的方法名称长度为 49 个字符

修复

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

于 2018-05-20T10:59:45.847 回答
15

如果您是想要忽略这一点的 Visual Studio Code 用户,您可以python.linting.pylintArgs添加.vscode/settings.json

{
    ...
    "python.linting.pylintArgs": [
        "--disable=C0103"
    ]
    ...
}
于 2019-10-21T02:44:48.390 回答
8

@jrtapsell 指出的很好

添加更多信息:

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

您可能会注意到名称的长度及其正则表达式可以在 2 到 30 个字符之间变化。

    +-------------------+---------------+-------------------------------------------+
    |       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

于 2019-12-28T20:24:59.607 回答
0

此外,如果您尚未生成 .pylinrc 文件,则可以使用以下命令生成。

pylint --generate-rcfile | out-file -encoding utf8 .pylintrc

然后您可以在 .pylinrc 文件中更改命名案例的类型,这里有一些流行案例和示例用例。

PascalCase: NewObject camelCase: newObject PascalCase: LongFunctionName() camelCase: longFunctionName()

于 2020-12-16T23:11:52.590 回答
0

每当您遇到此类错误时,请注意该行。您需要以 snake_case 样式提及您的函数名称。这意味着

"def TddInPythonExample():": ->  def dd_in_python_example():
于 2021-04-27T20:31:43.220 回答