Skip to content

Pylint Configuration

Niayesh Ilkhani edited this page Oct 29, 2016 · 8 revisions

Configuring Pylint Using Options

Pylint features can be enabled, disabled, and altered using a pylintrc file. This file contains configuration options that Pylint interprets using the Python standard configparser library. These features can also be altered when pylint is run directly from the command line, using command line arguments starting with --, which pylint interprets using the optparse library (now deprecated, replaced with argparse) and stores using the configparser library.

Writing Configuration Options

The complete list of pylint features that can be altered using options/switches can be found on the official pylint documentation. (You can see examples of the exact syntax by running pylint --generate-rcfile in the command line, or by checking pylint's pylintrc file on Github.) Most importantly, however, command line flags should be prefixed with -- (or just - for shortened commands), but this prefix should be omitted when specifying commands in a pylintrc file.

Options in the Command Line

Since pylint has been designed ideally to be used from the command line, it is simple to configure the running of the tool after calling pylint but before passing the name of the module to be checked. The typical format of a call to pylint, then, will appear like this:

pylint --<option-1-name>=<option-1-value> --<switch-name> <module name>

Pylint can also be called with commands that do not start a check, but rather return information, such as --version, --help (or -h), --generate-rcfile, --list-msgs, and --full-documentation.

Options in a pylintrc File

The same options that pylint can be configured with from the command line can also be set in a pylintrc (or .pylintrc) file. Pylint can also automatically generate a properly-formatted pylintrc file: in the command line, calling --generate-rcfile will standard-output the default option values along with any changes specified with options appearing before --generate-rcfile in the command line.

How Pylint Finds pylintrc Files

Pylint's config.py can search automatically for pylintrc (or .pylintrc) files within your computer's file system. However, pylint only checks the usual locations:

  • the current directory pylint is running in,
  • any parent directories of the current directory that contain an __init__.py file (that is, are recognisable by Python as package directories),
  • the PYLINTRC environment variable (if given),
  • the user's home directory or within .config in the home directory, or
  • the etc directory within the current directory (if it exists).

If the pylintrc file is not contained in any of the following locations, pylint should be called with the location of a pylintrc file explicitly stated using the --rcfile=<config file> option; otherwise, pylint will use the default configuration (plus options from the command line, if any).

Classes that Process Configuration Options

PyLinter

This is the main checker amongst all checkers. It overrides/extends OptionsManagerMixIn.set_option to add config options not internal to other checkers: enable and disable, and add a custom reporter (uses PyLinter._load_reporter()).

OptionsManagerMixIn

Extended by PyLinter to provide config file reading / option storing functionality. Uses ConfigParser to parse the pylintrc configuration file, if one exists.

Main method: set_option: Stores checker options in an optparse.Values object config. This method is overridden by its subclasses that want to add options additional to the default ones (that is, by checkers).

OptionsProviderMixIn

Extended by checkers to provide option storing and managing functionality. Checkers access the sections attribute defined by an OptionsProviderMixIn to look for options they should follow. Reporters do not inherit from OptionsProviderMixIn, so they cannot directly request custom options. However, the pylintrc example file does indicate a msg-template option that can be used to customise the message format that reporters output, which is accessed by TextReporter and HTMLReporter using self.linter.config.msg_template. Aside from this seemingly hard-coded exception though, pylint reporters do not have a mechanism to set up custom options as checkers do.

Things That Could Change

  • python_ta._check does lots of things that a pylintrc file can do:
    • reporter: use --output-format=<reporter module>
    • level: currently sent to reporter's print_messages, but as mentioned on the reporters page, reporters should actually override BaseReporter.display_messages. Also, level is currently used to indicate "display code errors, display style errors, or all", but this should instead be given using --disable=<category> in pylintrc, where <category> is one of "C, W, E, F, R, standing for Convention, Warning, Error, Fatal and Refactoring" (Pylint docs).
    • local_config_file: doesn't need to be indicated in pylintrc, but can be indicated in the command line using --rcfile=<config file>.
Clone this wiki locally