Skip to content

Pylint Configuration

Niayesh Ilkhani edited this page Nov 3, 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).

Altering pylintrc Options in the Command Line

Sometimes, you might need to use all the options defined in an existing pylintrc file, with a few alterations. This can be done in the command line:

pylint --<option-1>=<new-value-1> --<option-2>=<new-value-2> --rcfile=<current pylintrc> --generate-rcfile

This will work to override the values of option-1 and option-2, even if they are defined in the given pylintrc. However, in order for this to work, the overridden options should come before the --rcfile command in the command line.

Pylint Classes that Process Configuration Options

PyLinter (source)

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 (source)

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 (source)

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.

How Python TA Uses Options

Currently (as of Oct. 2016), Python TA has three custom checkers that have their own options (which can be altered from the default by adding to the Python TA pylintrc file):

  • IOFunctionChecker options:
    • forbidden-io-functions: input/output functions that must not be used in student code.
      • Default: input, print, and open.
      • Alter this using forbidden-io-functions=<comma-separated function names>.
    • allowed-io: forbidden input/output functions that are being overridden as allowed.
      • Default: none.
      • Alter this using allowed-io=<comma-separated function names>.
  • ForbiddenImportChecker options:
    • allowed-import-modules: modules that are permitted to be imported by student code.
      • Default: none (no modules allowed to be imported).
      • Alter this using 'allowed-import-modules=`.
  • DynamicExecutionChecker options:
    • forbidden-dynamic-exec: dynamic execution functions that are not permitted to be called by student code.
      • Default: compile, eval, and exec.
      • Alter this using forbidden-dynamic-exec=<comma-separated function names>.
Clone this wiki locally