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)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user1050619
  • 19,822
  • 85
  • 237
  • 413

5 Answers5

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

Just a nice guy
  • 549
  • 3
  • 19
jrtapsell
  • 6,719
  • 1
  • 26
  • 49
  • 8
    https://stackoverflow.com/a/48893283/3527520 contains a note about how to do the config change – Simon Fraser May 20 '18 at 11:00
  • 2
    If you just want to specify an exception to the rule: # pylint: disable=invalid-name – arhuaco Aug 21 '19 at 00:14
  • 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. – moken Jul 23 '23 at 13:22
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"
    ]
    ...
}
Bartleby
  • 1,144
  • 1
  • 13
  • 14
9

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_]*$                   |
    +-------------------+---------------+-------------------------------------------+
Just a nice guy
  • 549
  • 3
  • 19
Akarsh Jain
  • 930
  • 10
  • 15
  • 1
    The link points to a Chinese Language only site which is not suitable for inclusion in an answer on this site. – moken Jul 23 '23 at 13:42
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()

Wester king
  • 103
  • 1
  • 1
  • 5
-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():
Sindhukumari P
  • 324
  • 2
  • 6