Skip to content

Pylint Configuration

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

Introduction

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). In fact, features passed to pylint in the command line can be output as a properly-formatted pylintrc file using the pylint option --generate-rcfile.

Writing Configuration Options

Options in the Command Line

Options in a pylintrc File

How Pylint Finds pylintrc Files

Pylint's config.py can search automatically for pylintrc 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,
  • the user's home directory, or
  • the etc directory within the current directory.

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; otherwise, pylint will use the default configuration (plus options from the command line, if any).

Classes that Process Configuration Options

PyLinter

Overrides/extends OptionsManagerMixIn.set_option to add config options not internal to checkers: enable and disable, and adding 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.

OptionsProviderMixIn

Extended by checkers to provide option storing? and managing functionality.

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 for obvious reasons, but can be indicated in the command line using --rcfile=<config file>.

How Pylint Works (TODO: move)

So usually you call Pylint from CMD, which means you do something like:

> pylint --option-1 --option-2 <module to check>

And this makes a call to __main__.py, which calls pylint.run_pylint() (inside __init__.py). run_pylint then calls pylint.lint.Run(sys.argv[1:]), where sys.argv[1:] is every command line argument except the call to pylint itself. Unless only calling pylint for information, the last element of sys.argv is the module or filename to be linted. Run in turn loads up the PyLinter: * Pre-processing (command line): the init-hook (standalone Python code), rcfile, and load-plugins command line options are processed first, because following parts of the program may be dependent on them. preprocess_options searches the arguments from argv for those three options, and calls their associated callback functions (as defined by Run) to load the values from argv into variables used by the linter. * Initialising PyLinter: Call PyLinter.__init__ with a series of "external" options (and option groups), as well as the rcfile found. * PyLinter then adds to this list of options and option groups, and amongst its other tasks (several relating to checkers and reporters), initialises OptionsManagerMixIn with the path to the pylintrc file. * Reading into the linter: Register all checkers, plugins, help documents, etc. Then run read_config_file() (an OptionsManagerMixIn method) on the linter to read and record (but not run) the options in the pylintrc file. * OptionsManagerMixIn is responsible for handling configurations (from both command line and pylintrc). Its read_config_file method uses a ConfigParser to read the pylintrc file, which stores the sections, keys, and values from the file in _sections. * Pre-processing (config file): As before when pre-processing the command line options, the config file can have init-hook and load-plugins options that must be executed before any other options. * Loading the linter: Call OptionsManagerMixIn.load_config_file() to set each option up. * load_config_file adds every registered/read to the list of options to be followed when the linter is linting. * Loading command line options: Alter configuration based on command line arguments using load_command_line_configurations(args).

utils.register_plugins registers checkers and reporters (as called by the __init__s of checkers and reporters) by running their register method.

Other Info

  • Default pylint sections: ['MASTER', 'MESSAGES CONTROL', 'REPORTS', 'LOGGING', 'MISCELLANEOUS', 'SIMILARITIES', 'VARIABLES', 'FORMAT', 'BASIC', 'TYPECHECK', 'SPELLING', 'DESIGN', 'CLASSES', 'IMPORTS', 'EXCEPTIONS']
  • Current PyTA sections: ['ELIF', 'FORMAT', 'FORBIDDEN IMPORT', 'FORBIDDEN IO', 'MESSAGES CONTROL']
Clone this wiki locally