Disable invalid name arguments pylint
Question:
I have code like this:
def func(df):
return df.column[0]
I am running pylint and it gives me this message all the time because it flags df as an invalid name despite it’s convention.
C:210, 9: Invalid variable name "df" (invalid-name)
Where 210 refers to the row number (not the message code)
Pylint seems to say I can exclude messages by id type but:
- It doesn’t seem to list the message id, just the row and column numbers
- I don’t see where I would exclude messages for a specific variable name but not warnings for other variable names.
Answers:
In your pylintrc
file you can define a good-names
variable with a comma seperated list of acceptable names that would otherwise fail the regex.
# Good variable names which should always be accepted, separated by a comma
good-names=i,j,df
If you do not already have a pylintrc
file for your project you can generate a template to start with by running:
pylint --generate-rcfile > pylintrc
If you want to allow any 1 or 2 length variable names, you can add/edit this line in the pylintrc
file:
# allow 1 or 2 length variable names
good-names-rgxs=^[_a-z][_a-z0-9]?$
^ # starts with
[_a-z] # 1st character required
[_a-z0-9]? # 2nd character optional
$ # ends with
If you just want to have a whitelist of allowed variable names, then add/edit this line in the pylintrc
file:
good-names=ax, df
If you want to keep all your settings in pyproject.toml
:
[tool.pylint]
good-names="ls,rm"
I have code like this:
def func(df):
return df.column[0]
I am running pylint and it gives me this message all the time because it flags df as an invalid name despite it’s convention.
C:210, 9: Invalid variable name "df" (invalid-name)
Where 210 refers to the row number (not the message code)
Pylint seems to say I can exclude messages by id type but:
- It doesn’t seem to list the message id, just the row and column numbers
- I don’t see where I would exclude messages for a specific variable name but not warnings for other variable names.
In your pylintrc
file you can define a good-names
variable with a comma seperated list of acceptable names that would otherwise fail the regex.
# Good variable names which should always be accepted, separated by a comma
good-names=i,j,df
If you do not already have a pylintrc
file for your project you can generate a template to start with by running:
pylint --generate-rcfile > pylintrc
If you want to allow any 1 or 2 length variable names, you can add/edit this line in the pylintrc
file:
# allow 1 or 2 length variable names
good-names-rgxs=^[_a-z][_a-z0-9]?$
^ # starts with
[_a-z] # 1st character required
[_a-z0-9]? # 2nd character optional
$ # ends with
If you just want to have a whitelist of allowed variable names, then add/edit this line in the pylintrc
file:
good-names=ax, df
If you want to keep all your settings in pyproject.toml
:
[tool.pylint]
good-names="ls,rm"