Last updated: Apr 11, 2024
Reading time·4 min
invalid-name
warning for a single lineinvalid-name
warning globally in pylintrc
The following Pylint warnings are all related:
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.
# ✅ Correct my_variable = 'bobbyhadz.com'
The following is incorrect.
# ⛔️ 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.
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:
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.
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.
invalid-name
warning for a single lineIf you want to disable the Pylint invalid-name
warning for a single line, use
a comment.
helloWorld = 'bobbyhadz.com' # pylint: disable=invalid-name
You can also use the following comment to achieve the same result.
# pylint: disable-next=invalid-name helloWorld = 'bobbyhadz.com'
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.
[MESSAGES CONTROL] disable=invalid-name
I've written a detailed guide on how to disable specific Pylint warnings.
invalid-name
warning in Visual Studio CodeIf you use Visual Studio Code as your IDE, you can also disable the
invalid-name
warning directly in VS Code.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
invalid-name
warning to the python.linting.pylintArgs
list.{ "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 Code project.
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.
{ "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 configuration
settings.
When writing code in Python, the convention is for class names to be PascalCase and method names to be snake_case.
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.
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.
full_name = 'bobby hadz'
Function and method arguments should also follow the snake_case
convention.
self
for the first argument to instance methods.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.
You can learn more about the related topics by checking out the following tutorials: