【问题标题】:Method name doesn't conform to snake_case naming style方法名不符合snake_case 命名风格
【发布时间】:2018-05-20 10:46:42
【问题描述】:

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

【问题讨论】:

  • 错误是什么?
  • 这是一个警告,如主题所示。
  • 对于 pylint 的维护者来说,这可能比作为一个 SO 问题更好的错误报告;该方法名称似乎确实具有正确的样式。他们可能还想查看有问题的配置文件。
  • 即使我认为这个名字看起来风格正确,只是想确认一下......将向 pylint.thanks 报告一个错误。

标签: python pylint pylintrc


【解决方案1】:

为什么方法名被拒绝

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

修复

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

【讨论】:

【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    @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

    【讨论】:

      【解决方案4】:

      另外,如果您还没有生成 .pylinrc 文件,您可以使用以下命令生成 命令。

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

      然后你可以在 .pylinrc 文件中更改命名大小写的类型, 以下是一些流行案例和示例用例。

      PascalCase:新对象 骆驼案例:新对象 PascalCase:LongFunctionName() camelCase: longFunctionName()

      【讨论】:

        【解决方案5】:

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

        "def TddInPythonExample():": ->  def dd_in_python_example():
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-11-10
          • 1970-01-01
          • 2014-05-19
          • 1970-01-01
          • 1970-01-01
          • 2018-01-31
          • 2011-07-29
          相关资源
          最近更新 更多