Method name doesn't conform to snake_case naming style
Asked Answered
C

5

23

I’m creating a simple project with my pylintrc file and get this error for the test method:

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)
Cupric answered 20/5, 2018 at 10:46 Comment(5)
what is the error ?Galligaskins
its a warning, as shown on the subject.Cupric
This might be better as a bug report to pylint’s maintainers than an SO question; that method name does appear to have the right style. They’ll also probably want to see the config file in question.Borgeson
even I thought the name appear to be in the right style, just want to confirm...will report a bug to pylint.thanks.Cupric
side note: this page describes the snake_case naming style en.wikipedia.org/wiki/Snake_caseIcosahedron
C
34

Why is the method name rejected

It appears according to this: (Link removed) that the length of the name is capped at 30 characters, where your method name is 49 characters long

The fix

You can shorten the method name, or change your config to allow longer methods

Chin answered 20/5, 2018 at 10:59 Comment(3)
https://mcmap.net/q/584331/-pylint-invalid-function-name contains a note about how to do the config changeMultiplicity
If you just want to specify an exception to the rule: # pylint: disable=invalid-nameRehearse
The link in this answer points to what appears to be a Chinese language site so is not appropriate to be used in an answer for this site. The link itself also appears to be broken so even if it linked to a section in English that is no longer the case.Popularize
J
23

If you are a Visual Studio Code user who wants to ignore this, you can add python.linting.pylintArgs to .vscode/settings.json:

{
    ...
    "python.linting.pylintArgs": [
        "--disable=C0103"
    ]
    ...
}
Janae answered 21/10, 2019 at 2:44 Comment(0)
A
10

Very well pointed by @jrtapsell

To Add further information:

There is a regular expression defined for each type when it comes to naming convention.

You may note the length of a name can vary from 2 to 30 characters along with its 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_]*$                   |
    +-------------------+---------------+-------------------------------------------+
Ashling answered 28/12, 2019 at 20:24 Comment(2)
The link points to a Chinese Language only site which is not suitable for inclusion in an answer on this site.Popularize
Just FYI, the regular expression for the method name is actually 3-31 characters long. The first character, then 2-30 characters after that.Potato
F
0

Also, if you have not generated the .pylinrc file, you can do so using the following command.

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

then you can change the type of naming case in .pylinrc file, here are some popular cases and sample use case.

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

Farrier answered 16/12, 2020 at 23:11 Comment(0)
T
-1

Note that line whenever you get this kind of error. you need to mention your function name in snake_case style. That means

"def TddInPythonExample():": ->  def dd_in_python_example():
Thyratron answered 27/4, 2021 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.