pycodestyle 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 pycodestyle.StyleGuide(parse_argv=False, config_file=None, parser=None, paths=None, report=None, **kwargs)[source]

Initialize a PEP-8 instance with few options.

init_report(reporter=None)[source]

Initialize the report instance.

check_files(paths=None)[source]

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.

input_dir(dirname)[source]

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.

ignore_code(code)[source]

Check if the error code should be ignored.

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

get_checks(argument_name)[source]

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 pycodestyle.Checker(filename=None, lines=None, report=None, **kwargs)[source]

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

readline()[source]

Get the next line from the input buffer.

run_check(check, argument_names)[source]

Run a check plugin.

check_physical(line)[source]

Run all physical checks on a raw input line.

build_tokens_line()[source]

Build a logical line from tokens.

check_logical()[source]

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

check_ast()[source]

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

generate_tokens()[source]

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 pycodestyle.BaseReport(options)[source]

Collect the results of the checks.

start()[source]

Start the timer.

stop()[source]

Stop the timer.

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

Signal a new file.

increment_logical_line()[source]

Signal a new logical line.

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

Report an error, according to options.

get_file_results()[source]

Return the count of errors and warnings for this file.

get_count(prefix='')[source]

Return the total count of errors and warnings.

get_statistics(prefix='')[source]

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_statistics(prefix='')[source]

Print overall statistics (number of errors and warnings).

print_benchmark()[source]

Print benchmark numbers.

class pycodestyle.FileReport(options)[source]

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

class pycodestyle.StandardReport(options)[source]

Collect and print the results of the checks.

class pycodestyle.DiffReport(options)[source]

Collect and print the results for the changed lines only.

Utilities

pycodestyle.expand_indent(line)[source]

Return the amount of indentation.

Tabs are expanded to the next multiple of 8.

>>> expand_indent('    ')
4
>>> expand_indent('\t')
8
>>> expand_indent('       \t')
8
>>> expand_indent('        \t')
16
pycodestyle.mute_string(text)[source]

Replace contents with ‘xxx’ to prevent syntax matching.

>>> mute_string('"abc"')
'"xxx"'
>>> mute_string("'''abc'''")
"'''xxx'''"
>>> mute_string("r'abc'")
"r'xxx'"
pycodestyle.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/pycodestyle) and any local configurations in the current directory or above will be merged together (in that order) using the read method of ConfigParser.

pycodestyle.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 pycodestyle.

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

Register a new check object.