pep8 API

The library provides classes which are usable by third party tools.

Checker Classes

The StyleGuide class is used to configure a style guide checker instance to check multiple files.

The Checker class can be used to check a single file.

class pep8.StyleGuide(parse_argv=False, config_file=None, parser=None, paths=None, report=None, **kwargs)[source]

Initialize a PEP-8 instance with few options.


Initialize the report instance.


Run all checks on the paths.

input_file(filename, lines=None, expected=None, line_offset=0)[source]

Run all checks on a Python source file.


Check all files in this directory and all subdirectories.

excluded(filename, parent=None)[source]

Check if the file should be excluded.

Check if ‘options.exclude’ contains a pattern that matches filename.


Check if the error code should be ignored.

If ‘’ contains a prefix of the error code, return False. Else, if ‘options.ignore’ contains a prefix of the error code, return True.


Get all the checks for this category.

Find all globally visible functions where the first argument name starts with argument_name and which contain selected tests.

class pep8.Checker(filename=None, lines=None, report=None, **kwargs)[source]

Load a Python source file, tokenize it, check coding style.


Get the next line from the input buffer.

run_check(check, argument_names)[source]

Run a check plugin.


Run all physical checks on a raw input line.


Build a logical line from tokens.


Build a line from tokens and run all logical checks on it.


Build the file’s AST and run all AST checks.


Tokenize the file, run physical line checks and yield tokens.

check_all(expected=None, line_offset=0)[source]

Run all checks on the input file.

Report Classes

class pep8.BaseReport(options)[source]

Collect the results of the checks.


Start the timer.


Stop the timer.

init_file(filename, lines, expected, line_offset)[source]

Signal a new file.


Signal a new logical line.

error(line_number, offset, text, check)[source]

Report an error, according to options.


Return the count of errors and warnings for this file.


Return the total count of errors and warnings.


Get statistics for message codes that start with the prefix.

prefix=’’ matches all errors and warnings prefix=’E’ matches all errors prefix=’W’ matches all warnings prefix=’E4’ matches all errors that have to do with imports


Print overall statistics (number of errors and warnings).


Print benchmark numbers.

class pep8.FileReport(options)[source]

Collect the results of the checks and print only the filenames.

class pep8.StandardReport(options)[source]

Collect and print the results of the checks.

class pep8.DiffReport(options)[source]

Collect and print the results for the changed lines only.



Return the amount of indentation.

Tabs are expanded to the next multiple of 8.

>>> expand_indent('    ')
>>> expand_indent('\t')
>>> expand_indent('       \t')
>>> expand_indent('        \t')

Replace contents with ‘xxx’ to prevent syntax matching.

>>> mute_string('"abc"')
>>> mute_string("'''abc'''")
>>> mute_string("r'abc'")
pep8.read_config(options, args, arglist, parser)[source]

Read and parse configurations

If a config file is specified on the command line with the “–config” option, then only it is used for configuration.

Otherwise, the user configuration (~/.config/pep8) and any local configurations in the current directory or above will be merged together (in that order) using the read method of ConfigParser.

pep8.process_options(arglist=None, parse_argv=False, config_file=None)[source]

Process options passed either via arglist or via command line args.

Passing in the config_file parameter allows other tools, such as flake8 to specify their own options to be processed in pep8.

pep8.register_check(func_or_cls, codes=None)[source]

Register a new check object.