Question

class MyFavoriteClass():
  def __init__(self):
      self.counter = 0
  def memberFunction(self):
      self.counter = self.counter + 1

myinstance = MyFavoriteClass() #Pylint complains here
myinstance.memberFunction()

The error reported by pylint for the second to last line is

[C0103] Invalid name "myinstance" for type constant (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)

I have read that it is possible to disable this type of error entirely, but is it necessary?

How can I tell pylint that myinstance is not a constant?

System configuration reported by pylint -v

pylint 0.26.0, 
astng 0.24.1, common 0.59.1
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1]
Was it helpful?

Solution

With pylint, you can ignore individual errors, warnings, etc. on a line by using a special comment of the format: # pylint: disable={comma-separated-list-of-names-or-codes}

class MyFavoriteClass():
    def __init__(self):
        self.counter = 0
    def memberFunction(self):
        self.counter = self.counter + 1

myinstance = MyFavoriteClass() # pylint: disable=invalid-name
myinstance.memberFunction()

Or, you can specify a list of message codes to ignore in the pylint config file:

[MESSAGES CONTROL]

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
disable=invalid-name
# OR
disable=C0103

NOTE: Disabling a code from the config file will ignore it globally. If you want to just ignore it on a specific line, you'll have to use the special comment mentioned at the beginning.

The pylint config file is determined by first checking the PYLINTRC environment variable. If that does not provide a file, then ~/.pylintrc and /etc/pylintrc are checked consecutively.

If you can control the pylint command being executed, you can also specify the config using the --rcfile parameter.

If you'd like to generate a sample config, run:

pylint --generate-rcfile

Additionally, if you disable a code, that will trigger locally-disabled (I0011) which itself can also disabled (ideally in the config).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top