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:

  1. It doesn’t seem to list the message id, just the row and column numbers
  2. I don’t see where I would exclude messages for a specific variable name but not warnings for other variable names.
Asked By: Chris

||

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
Answered By: Kara

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
Answered By: wisbucky

If you want to keep all your settings in pyproject.toml:

[tool.pylint]
good-names="ls,rm"
Answered By: flipback
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.