Skip to content

Latest commit

 

History

History
171 lines (125 loc) · 5.43 KB

File metadata and controls

171 lines (125 loc) · 5.43 KB

Flake 8

See full documentation

Flake8 is a wrapper around these tools:

  • PyFlakes
  • pycodestyle
  • Ned Batchelder's McCabe script

Summary

Prerequisites

To install this tool, you will need:

  • Python library
    • local
    • from a Docker container
  • pip (already installed if you are using Python 2 >=2.7.9 or Python 3 >=3.4))

Windows

Microsoft Visual C++ 14.0 (2015) with C++ Build Tools is required before installing pip. Note that while the error is calling for vc++ 14.0 - everything will work with newer versions of visual C++.

Install

It is very important to install Flake8 on the correct version of Python for your needs. If you want Flake8 to properly parse new language features in Python 3.5 (for example), you need it to be installed on 3.5 for Flake8 to understand those features. In many ways, Flake8 is tied to the version of Python on which it runs.

To install Flake8, open an interactive shell and run:

python<version> -m pip install flake8

If you want Flake8 to be installed for your default Python installation, you can instead use:

python -m pip install flake8

pip will install Flake8 in C:\Users\<username>\AppData\Local\Programs\Python\Python39\Scripts.

Configuration

From a file

Flake8 supports storing its configuration in the following places:

  • Your top-level user directory
  • In your project in one of setup.cfg, tox.ini, or .flake8.

Note that Flake8 looks for ~\.flake8 on Windows and ~/.config/flake8 on Linux and other Unix systems.

Values set at the command line have highest priority, then those in the project configuration file, then those in your user directory, and finally there are the defaults. However, there are additional command line options which can alter this.

Configuration format

Regardless of whether you keep your config in .flake8, setup.cfg, or tox.ini we expect you to use INI to configure Flake8 (since each of these files already uses INI as a format). This means that any Flake8 configuration you wish to set needs to be in the flake8 section.

Configuration example:

[flake8]
ignore = D203
exclude =
    # No need to traverse our git directory
    .git,
    # There's no value in checking cache directories
    __pycache__,
    # The conf file is mostly autogenerated, ignore it
    docs/source/conf.py,
    # The old directory contains Flake8 2.0
    old,
    # This contains our built documentation
    build,
    # This contains builds of flake8 that we don't want to check
    dist
max-complexity = 10
'''

PyCharm

External tool

By installing the tools globally on a development machine, it is possible to create an external tool in Pycharm

In Settings > Tools > External Tools, create a new tool with the following settings:

Key Value
Program C:\Users<username>\AppData\Local\flake8
Arguments $ProjectFileDir$\src
Working directory $ProjectFileDir$

Usage

To get started right away with sensible defaults:

flake8 {source_file_or_directory}

If you have installed Flake8 on a particular version of Python (or on several versions), it may be best to instead run:

python<version> -m flake8 {source_file_or_directory}

f you only want to see the instances of a specific warning or error, you can select that error like so:

flake8 --select E123,W503 {source_file_or_directory}

Alternatively, if you want to ignore only one specific warning or error:

flake8 --ignore E24,W504 {source_file_or_directory}

Version control integration

Flake8 can be included as a hook for pre-commit. The easiest way to get started is to add this configuration to your .pre-commit-config.yaml:

    -   repo: https://gitlab.com/pycqa/flake8
        rev: ''  # pick a git hash / tag to point to
        hooks:
        -   id: flake8

See the pre-commit docs for how to customize this configuration.

Checked-in python files will be passed as positional arguments. flake8 will always lint explicitly passed arguments (:option:flake8 --exclude has no effect). Instead use pre-commit's exclude: ... regex to exclude files. pre-commit won't ever pass untracked files to flake8 so excluding .git / .tox / etc. is unnecessary.

-   id: flake8
    exclude: ^testing/(data|examples)/

pre-commit creates an isolated environment for hooks. To use flake8 plugins, use the additional_dependencies setting.

-   id: flake8
    additional_dependencies: [flake8-docstrings]