# Table of Contents
- Variable name doesn't conform to snake_case naming style
- Disabling the invalid-name warning for a single line
- Disabling the invalid-name warning globally in pylintrc
- Class names must be PascalCase, method and function names must be snake_case
- 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
By default, Pylint enforces thePEP8-suggested names.
For example, variable names must use snake_case
.
The following is correct because snake_case
is used.
main.py
Copied!
# ✅ correctmy_variable = 'bobbyhadz.com'
The following is incorrect.
main.py
Copied!
# ⛔️ 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 aname 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 thefollowing table.
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_]*$ |
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 30characters).
This makes a minimum of 3 characters for argument names.
If you want to have variable or function names that are fewer than 3 charactersor longer than 30 characters, you have to edit the regular expression in yourpylintrc
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 wouldadd the following to your pylintrc
file.
pylintrc
Copied!
argument-rgx=[a-z\_][a-z0-9_]{0,39}$
I've written more on how to edit your pylintrc
file or generate one inthis article.
# Disabling the invalid-name
warning for a single line
If you want to disable the Pylint invalid-name
warning for a single line, usea comment.
main.py
Copied!
helloWorld = 'bobbyhadz.com' # pylint: disable=invalid-name
You can also use the following comment to achieve the same result.
main.py
Copied!
# pylint: disable-next=invalid-namehelloWorld = 'bobbyhadz.com'
# Disabling the invalid-name
warning globally in pylintrc
If you want to disable the invalid-name
Pylint rule globally, you can create apylintrc
file in the root of your project and add the rule to the disable=
key.
pylintrc
Copied!
[MESSAGES CONTROL]disable=invalid-name
I've written a detailed guide on how todisable 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 theinvalid-name
warning directly in VS Code.
- Press
Ctrl
+Shift
+P
(orCommand
+Shift
+P
on macOS).
Note: you can also press F1
to open the Command Palette.
Type user settings json.
Click on Preferences: Open User Settings (JSON)
- Add the
invalid-name
warning to thepython.linting.pylintArgs
list.
settings.json
Copied!
{ "python.linting.pylintArgs": [ "--disable=invalid-name" ],}
Each item in the list is formatted as "--disable=PYLINT_WARNING_NAME".
You can also disable the warning only for the current Visual Studio Codeproject.
In the root directory of your project, create a
.vscode
folder.Create a
settings.json
file in the.vscode
folder.Add the following code to your
settings.json
file.
.vscode/settings.json
Copied!
{ "python.linting.pylintArgs": [ "--disable=invalid-name" ]}
Note that the configuration properties in your local .vscode/settings.json
file only apply to your current project and overwrite any global configurationsettings.
# 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 PascalCaseand method names to be snake_case.
main.py
Copied!
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 thefirst), e.g. WebDeveloper
or Developer
.
Method and function names must be snake_case.
Function and method names are written in lowercase with words separated byunderscores to improve readability.
main.py
Copied!
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
Copied!
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 inthis sectionof the PEP8 style guide.
# Additional Resources
You can learn more about the related topics by checking out the followingtutorials:
- Consider explicitly re-raising using 'raise Error from'
- Linter pylint is not installed error in VS Code [Solved]
- Code is unreachable warning in Python [Solved]
- How to disable/suppress Tensorflow warnings in Python
- String statement has no effect Pylint (pointless-string-statement)
- Pylint unused-variable, unused-argument, unused-import [Fix]