Skip to content

Latest commit

 

History

History
58 lines (37 loc) · 1.89 KB

File metadata and controls

58 lines (37 loc) · 1.89 KB

Flake8

Overview

Tutorial

What is Flake8?

Flake8 is a powerful Python tool that checks your code against coding style (PEP8), programming errors (like "syntax errors"), and complexity issues. It's an essential tool for maintaining clean, consistent, and maintainable Python code.

Why Use Flake8 with Pre-Commit?

Integrating Flake8 as a pre-commit hook ensures that every commit adheres to your project's style and linting guidelines. This prevents errors or bad style from slipping into your codebase.

Installation via Pre-Commit Hook

To integrate Flake8 into your repository using pre-commit, add the following configuration to your .pre-commit-config.yaml file:

repos:
  - repo: https://github.com/pre-commit/mirrors-flake8
    rev: v6.1.0  # Replace with the latest version
    hooks:
      - id: flake8

Customizing Flake8

To customize Flake8, you can add a configuration file named setup.cfg, tox.ini, or .flake8 in your project root:

[flake8]
max-line-length = 88
ignore = E203, E266, E501, W503
exclude = .git,__pycache__,old,build,dist
  • max-line-length: Sets the maximum line length (e.g., 88 for compatibility with Black).
  • ignore: Ignore specific warnings or errors (e.g., Black compatibility).
  • exclude: Exclude specific directories or files from linting.

Running Flake8 Manually

If needed, you can manually run Flake8 locally:

flake8 path/to/your/code/

Advanced Tips

  • Combine Flake8 with tools like isort and Black in your pre-commit setup for comprehensive formatting and linting.
  • Use Flake8 plugins like flake8-bugbear to catch common issues or flake8-comprehensions for optimizing comprehensions.