17

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.
Chris
  • 12,900
  • 12
  • 43
  • 65
  • note that the regular expression pylint uses for validating variables names is `[a-z_][a-z0-9_]{2,30}$`, meaning that `"df"` is in fact an invalid name. – Hamms Jun 13 '16 at 21:08
  • The error you're getting is error C0103, also known as "invalid-name". It can be disabled using either of those identifiers – Hamms Jun 13 '16 at 21:10
  • 3
    Yes, I know but df is a common convention in Pandas that I don't want to break away from. Also 'ax' in Matplotlib so I want to exclude messages for those variable names. – Chris Jun 13 '16 at 21:13
  • 2
    I don't want to disable ALL 'invalid-name' warnings, just the warnings for those particular names since they are conventions. – Chris Jun 13 '16 at 21:14
  • There are several ways to do this, did you bother reading [the manual](https://docs.pylint.org/faq.html#message-control)? – jonrsharpe Jun 13 '16 at 21:34

3 Answers3

25

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
Kara
  • 6,115
  • 16
  • 50
  • 57
  • According to this [answer](https://stackoverflow.com/a/35182977/7446465) the `good-names` option should go under the `[FORMAT]` section of pylintrc – Addison Klinke Oct 12 '21 at 13:51
4

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
wisbucky
  • 33,218
  • 10
  • 150
  • 101
2

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

[tool.pylint]
good-names="ls,rm"
atimin
  • 499
  • 4
  • 11