Variable name doesn't conform to snake_case naming style

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. Variable name doesn't conform to snake_case naming style
  2. Disabling the invalid-name warning for a single line
  3. Disabling the invalid-name warning globally in pylintrc
  4. Class names must be PascalCase, method and function names must be snake_case
  5. Function and method arguments should be snake_case

# Variable name doesn't conform to snake_case naming style

The following Pylint warnings are all related:

  • Variable name doesn't conform to snake_case naming style
  • Class name "example" doesn't conform to PascalCase naming style
  • Argument name "a" doesn't conform to snake_case naming style
  • Method name doesn't conform to snake_case naming style
  • Constant name "helloWorld" doesn't conform to snake_case naming style

variable name doesnt conform to snake case naming convention

By default, Pylint enforces the PEP8-suggested names.

For example, variable names must use snake_case.

The following is correct because snake_case is used.

main.py
# ✅ Correct my_variable = 'bobbyhadz.com'

The following is incorrect.

main.py
# ⛔️ Constant name "helloWorld" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]*|__.*__)$' pattern) pylint(invalid-name) helloWorld = 'bobbyhadz.com'

You might also run into issues when giving a variable, argument or a function a name that is fewer than 3 characters or more than 30 characters.

Pylint has default regular expressions that are validated against all names.

The length of names can vary between 2 and 30 characters as shown in the following table.

TypeOptionDefault regular expression
Argumentargument-rgx[a-z_][a-z0-9_]{2,30}$
Attributeattr-rgx[a-z_][a-z0-9_]{2,30}$
Classclass-rgx[A-Z_][a-za-z0-9]+$
Constantconst-rgx(([A-Z_][a-z0-9_]*) | (__.*__))$
Functionfunction-rgx[a-z_][a-z0-9_]{2,30}$
Methodmethod-rgx[a-z_][a-z0-9_]{2,30}$
Modulemodule-rgx(([a-z_][a-z0-9_]*) | ([A-Z][a-za-z0-9]+))$
Variablevariable-rgx[a-z_][a-z0-9_]{2,30}$
Variable, inline1inlinevar-rgx[A-Za-z_][a-za-z0-9_]*$

For example, the following regular expression [a-z\_][a-z0-9_]{2,30}$ means:

  • A letter or underscore
  • Followed by at least 2 letters, underscores or digits (or at most 30 characters).

This makes a minimum of 3 characters for argument names.

If you want to have variable or function names that are fewer than 3 characters or longer than 30 characters, you have to edit the regular expression in your pylintrc file.

The length of the names is specified between the curly braces in the regex {2,30}.

The Option column is the name of the key you have to set in your pylintrc file.

For example, if you want argument names to be from 1 to 40 characters, you would add the following to your pylintrc file.

pylintrc
argument-rgx=[a-z\_][a-z0-9_]{0,39}$

I've written more on how to edit your pylintrc file or generate one in this article.

# Disabling the invalid-name warning for a single line

If you want to disable the Pylint invalid-name warning for a single line, use a comment.

main.py
helloWorld = 'bobbyhadz.com' # pylint: disable=invalid-name

You can also use the following comment to achieve the same result.

main.py
# pylint: disable-next=invalid-name helloWorld = 'bobbyhadz.com'

# Disabling the invalid-name warning globally in pylintrc

If you want to disable the invalid-name Pylint rule globally, you can create a pylintrc file in the root of your project and add the rule to the disable= key.

pylintrc
[MESSAGES CONTROL] disable=invalid-name

disable invalid name warning pylintrc

I've written a detailed guide on how to disable specific Pylint warnings.

# Disabling the invalid-name warning in Visual Studio Code

If you use Visual Studio Code as your IDE, you can also disable the invalid-name warning directly in VS Code.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the invalid-name warning to the python.linting.pylintArgs list.
settings.json
{ "python.linting.pylintArgs": [ "--disable=invalid-name" ], }

disable invalid name warning in vscode

Each item in the list is formatted as "--disable=PYLINT_WARNING_NAME".

You can also disable the warning only for the current Visual Studio Code project.

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "python.linting.pylintArgs": [ "--disable=invalid-name" ] }

disable invalid name warning for current vscode project

Note that the configuration properties in your local .vscode/settings.json file only apply to your current project and overwrite any global configuration settings.

# Class names must be PascalCase, method and function names must be snake_case

When writing code in Python, the convention is for class names to be PascalCase and method names to be snake_case.

main.py
class WebDeveloper(): cls_id = 'web-developer' def __init__(self, first, last): self.first = first self.last = last def get_full_name(self): return f'{self.first} {self.last}'

Each word in the name of a class starts with a capital letter (including the first), e.g. WebDeveloper or Developer.

Method and function names must be snake_case.

Function and method names are written in lowercase with words separated by underscores to improve readability.

main.py
def get_full_name(first, last): return f'{first} {last}' # 👇️ "bobby hadz" print(get_full_name('bobby', 'hadz'))

Variable names follow the same convention as function names.

main.py
full_name = 'bobby hadz'

# Function and method arguments should be snake_case

Function and method arguments should also follow the snake_case convention.

  • Always use self for the first argument to instance methods.
  • Always use cls for the first argument to class methods.

You can read more about the naming conventions in Python in this section of the PEP8 style guide.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.