Skip to content

flake8

flake8 documentation

flake8 - GitHub

Configuration in Mega-Linter

Variable Description Default value
PYTHON_FLAKE8_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
PYTHON_FLAKE8_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
PYTHON_FLAKE8_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
PYTHON_FLAKE8_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
{linter.cli_lint_mode}
PYTHON_FLAKE8_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".py"]
PYTHON_FLAKE8_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
PYTHON_FLAKE8_PRE_COMMANDS List of bash commands to run before the linter None
PYTHON_FLAKE8_POST_COMMANDS List of bash commands to run after the linter None
PYTHON_FLAKE8_CONFIG_FILE flake8 configuration file name
Use LINTER_DEFAULT to let the linter find it
.flake8
PYTHON_FLAKE8_RULES_PATH Path where to find linter configuration file Workspace folder, then Mega-Linter default rules
PYTHON_FLAKE8_DISABLE_ERRORS Run linter but consider errors as warnings false
PYTHON_FLAKE8_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use flake8 in your favorite IDE to catch errors before Mega-Linter !

IDE Extension Name Install
Atom linter-flake8 Visit Web Site
IDEA flake8-support
Visual Studio Code Native Support Visit Web Site

Mega-Linter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default Mega-Linter Flavor 94 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 49 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .py

How the linting is performed

  • flake8 is called once with the list of files as arguments

Example calls

flake8 myfile.py
flake8 --config .flake8 myfile.py

Help content

usage: flake8 [options] file file ...

positional arguments:
  filename

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Print more information about what is happening in
                        flake8. This option is repeatable and will increase
                        verbosity each time it is repeated.
  --output-file OUTPUT_FILE
                        Redirect report to a file.
  --append-config APPEND_CONFIG
                        Provide extra config files to parse in addition to the
                        files found by Flake8 by default. These files are the
                        last ones read and so they take the highest precedence
                        when multiple files provide the same option.
  --config CONFIG       Path to the config file that will be the authoritative
                        config source. This will cause Flake8 to ignore all
                        other configuration files.
  --isolated            Ignore all configuration files.
  --version             show program's version number and exit
  -q, --quiet           Report only file names, or nothing. This option is
                        repeatable.
  --count               Print total number of errors and warnings to standard
                        error and set the exit code to 1 if total is not
                        empty.
  --diff                Report changes only within line number ranges in the
                        unified diff provided on standard in by the user.
  --exclude patterns    Comma-separated list of files or directories to
                        exclude. (Default: ['.svn', 'CVS', '.bzr', '.hg',
                        '.git', '__pycache__', '.tox', '.eggs', '*.egg'])
  --extend-exclude patterns
                        Comma-separated list of files or directories to add to
                        the list of excluded ones.
  --filename patterns   Only check for filenames matching the patterns in this
                        comma-separated list. (Default: ['*.py'])
  --stdin-display-name STDIN_DISPLAY_NAME
                        The name used when reporting errors from code passed
                        via stdin. This is useful for editors piping the file
                        contents to flake8. (Default: stdin)
  --format format       Format errors according to the chosen formatter.
  --hang-closing        Hang closing bracket instead of matching indentation
                        of opening bracket's line.
  --ignore errors       Comma-separated list of errors and warnings to ignore
                        (or skip). For example, ``--ignore=E4,E51,W234``.
                        (Default: ['E704', 'W504', 'E126', 'E123', 'E121',
                        'E24', 'W503', 'E226'])
  --extend-ignore errors
                        Comma-separated list of errors and warnings to add to
                        the list of ignored ones. For example, ``--extend-
                        ignore=E4,E51,W234``.
  --per-file-ignores PER_FILE_IGNORES
                        A pairing of filenames and violation codes that
                        defines which violations to ignore in a particular
                        file. The filenames can be specified in a manner
                        similar to the ``--exclude`` option and the violations
                        work similarly to the ``--ignore`` and ``--select``
                        options.
  --max-line-length n   Maximum allowed line length for the entirety of this
                        run. (Default: 79)
  --max-doc-length n    Maximum allowed doc line length for the entirety of
                        this run. (Default: None)
  --indent-size n       Number of spaces used for indentation (Default: 4)
  --select errors       Comma-separated list of errors and warnings to enable.
                        For example, ``--select=E4,E51,W234``. (Default: ['E',
                        'F', 'W', 'C90'])
  --extend-select errors
                        Comma-separated list of errors and warnings to add to
                        the list of selected ones. For example, ``--extend-
                        select=E4,E51,W234``.
  --disable-noqa        Disable the effect of "# noqa". This will report
                        errors on lines with "# noqa" at the end.
  --show-source         Show the source generate each error or warning.
  --no-show-source      Negate --show-source
  --statistics          Count errors and warnings.
  --enable-extensions ENABLE_EXTENSIONS
                        Enable plugins and extensions that are otherwise
                        disabled by default
  --exit-zero           Exit with status code "0" even if there are errors.
  -j JOBS, --jobs JOBS  Number of subprocesses to use to run checks in
                        parallel. This is ignored on Windows. The default,
                        "auto", will auto-detect the number of processors
                        available to use. (Default: auto)
  --tee                 Write to stdout and output-file.
  --benchmark           Print benchmark information about this run of Flake8
  --bug-report          Print information necessary when preparing a bug
                        report

mccabe:
  --max-complexity MAX_COMPLEXITY
                        McCabe complexity threshold

pyflakes:
  --builtins BUILTINS   define more built-ins, comma separated
  --doctests            also check syntax of the doctests
  --include-in-doctest INCLUDE_IN_DOCTEST
                        Run doctests only on these files
  --exclude-from-doctest EXCLUDE_FROM_DOCTEST
                        Skip these files when running doctests

Installed plugins: mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0

Installation on mega-linter Docker image

  • PIP packages (Python):

Example success log

Results of flake8 linter (version 3.8.4)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/python_flake8/
-----------------------------------------------

[SUCCESS] .automation/test/python/python_good_1.py


Example error log

Results of flake8 linter (version 3.8.4)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/python_flake8/
-----------------------------------------------

[ERROR] .automation/test/python/python_bad_1.py
    .automation/test/python/python_bad_1.py:6:13: E261 at least two spaces before inline comment
    .automation/test/python/python_bad_1.py:7:31: E261 at least two spaces before inline comment
    .automation/test/python/python_bad_1.py:8:16: E261 at least two spaces before inline comment
    .automation/test/python/python_bad_1.py:11:67: E202 whitespace before ')'
    .automation/test/python/python_bad_1.py:13:1: E128 continuation line under-indented for visual indent
    .automation/test/python/python_bad_1.py:15:23: E999 SyntaxError: invalid syntax
    .automation/test/python/python_bad_1.py:16:5: E113 unexpected indentation
    .automation/test/python/python_bad_1.py:17:9: E128 continuation line under-indented for visual indent
    .automation/test/python/python_bad_1.py:18:9: E128 continuation line under-indented for visual indent
    .automation/test/python/python_bad_1.py:27:1: E302 expected 2 blank lines, found 1
    .automation/test/python/python_bad_1.py:56:1: E302 expected 2 blank lines, found 1
    .automation/test/python/python_bad_1.py:66:24: E201 whitespace after '{'
    .automation/test/python/python_bad_1.py:87:1: E302 expected 2 blank lines, found 1
    .automation/test/python/python_bad_1.py:110:1: E302 expected 2 blank lines, found 1
    .automation/test/python/python_bad_1.py:153:1: E305 expected 2 blank lines after class or function definition, found 1
    .automation/test/python/python_bad_1.py:156:18: E261 at least two spaces before inline comment