PyLint: C0103 (invalid-name)

PyLint: C0103 (invalid-name)

A PyLint Convention message C0103 is raised anytime you use a name for something which does not conform to the python standard naming conventions. For example, you may get the following message if you name a method argument MyArg.

C0103: Argument name “MyArg” doesn’t conform to snake_case naming style (invalid-name)

There are a few clues here that help us figure out what is going wrong:

  • Argument refers to us defining something in a method signature.

  • MyArg lets us know the exact argument we have named incorrectly

  • snake_case lets us know we should be using all lower case with words separated by an underscore.

This is nice and easy to fix: we simply refactor-rename MyArg to my_arg and the message will disappear.

Problem example

The following code block has many different naming which violate the PyLint naming conventions. Can you spot what they all are?

MyConstant = 1
 
 
def MyFunc():
    return None
 
 
class myClass:
 
    def myMethod(self, MyArg):
        return self, [Var*2 for Var in MyArg]

If we run PyLint against the code snippet we get the following C0103 convention messages:

C0103: Constant name “MyConstant” doesn’t conform to UPPER_CASE naming style (invalid-name)
C0103: Function name “MyFunc” doesn’t conform to snake_case naming style (invalid-name)
C0103: Class name “myClass” doesn’t conform to PascalCase naming style (invalid-name)
C0103: Method name “myMethod” doesn’t conform to snake_case naming style (invalid-name)
C0103: Argument name “MyArg” doesn’t conform to snake_case naming style (invalid-name)

Resolved example

In the following code block we resolve each of the convention messages that PyLint printed.

MY_CONSTANT = 1
 
 
def my_func():
    return None
 
 
class MyClass:
 
    def my_method(self, my_arg):
        return self, my_arg

The following changes were made:

  • MyConstant refactor-renamed to MY_CONSTANT

  • MyFunc refactor-renamed to my_func

  • myClass refactor-renamed to MyClass

  • myMethod refactor-renamed to my_method

  • MyArg refactor-renamed to my_arg

Final thoughts

PyLint C0103 is a really helpful message to catch some really simple, obvious naming convention violations. However, it doesn’t catch everything that is defined within the official python naming conventions. For example, at time of writing, PyLint confuses a TypeVar as a Class name, and raises the naming convention message.

Good names make it easier for others to follow, maintain, and improve your code. It is always worth fixing these convention messages, and practicing writing code in a more pythonic way.