flake8
flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.
Top Related Projects
The uncompromising Python code formatter
It's not just a linter that annoys you!
An extremely fast Python linter and code formatter, written in Rust.
Simple Python style checker in one Python file
docstring style checker
A formatter for Python files
Quick Overview
Flake8 is a popular Python linting tool that combines the functionality of PyFlakes, pycodestyle, and McCabe complexity checker. It helps developers maintain code quality by checking for style issues, potential errors, and complexity in Python code.
Pros
- Easy to use and integrate into development workflows
- Highly configurable with many options and plugins available
- Combines multiple tools into a single package for comprehensive code checking
- Supports various output formats for easy integration with CI/CD pipelines
Cons
- Some users find the default configuration too strict
- Can produce false positives in certain scenarios
- Limited ability to automatically fix issues (unlike tools like Black)
- May require additional setup for more advanced configurations
Code Examples
- Basic usage:
# Run flake8 on a specific file
import subprocess
subprocess.run(["flake8", "my_file.py"])
- Ignoring specific errors:
# In your Python file
# flake8: noqa: E501
long_line = "This is a very long line that exceeds the default line length limit but will be ignored by flake8"
- Using flake8 as a pre-commit hook:
# In .pre-commit-config.yaml
repos:
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
Getting Started
To get started with flake8:
- Install flake8:
pip install flake8
- Run flake8 on your project:
flake8 .
- Create a configuration file (
.flake8
) in your project root:
[flake8]
max-line-length = 88
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
ignore = E203, E266, E501, W503
- Integrate flake8 into your development workflow (e.g., IDE, CI/CD pipeline, pre-commit hooks).
Competitor Comparisons
The uncompromising Python code formatter
Pros of Black
- Enforces a consistent code style with minimal configuration
- Automatically reformats code, reducing manual effort
- Faster execution compared to Flake8
Cons of Black
- Less flexible and customizable than Flake8
- May produce unexpected formatting in some cases
- Limited to code formatting, doesn't check for logical errors or style issues
Code Comparison
Black formatting:
def long_function_name(
var_one, var_two, var_three, var_four
):
print(f"{var_one} {var_two} {var_three} {var_four}")
Flake8 compatible formatting:
def long_function_name(var_one,
var_two,
var_three,
var_four):
print(f"{var_one} {var_two} {var_three} {var_four}")
Black enforces a specific style, while Flake8 allows for more flexibility in formatting. Black automatically wraps function arguments, while Flake8 leaves it to the developer's discretion, as long as it adheres to the specified line length limit.
Both tools serve different purposes: Black is primarily a code formatter, while Flake8 is a linter that checks for style issues and potential errors. Many developers use both tools in their workflow, with Black for formatting and Flake8 for additional style and error checking.
It's not just a linter that annoys you!
Pros of pylint
- More comprehensive analysis, including type checking and error detection
- Highly configurable with many options for customization
- Provides a numerical code quality score
Cons of pylint
- Slower performance, especially on larger codebases
- Can be overly strict, leading to many false positives
- Steeper learning curve due to its complexity
Code Comparison
pylint example:
# pylint: disable=C0103
x = 5 # Variable name "x" doesn't conform to snake_case naming style
flake8 example:
x = 5 # flake8 doesn't raise warnings for variable naming conventions by default
Both tools can check for common issues like unused imports or variables, but pylint goes further in its analysis. For instance, pylint can detect more complex issues like circular imports or inconsistent returns, which flake8 doesn't cover.
While pylint offers more extensive checks, flake8 is generally faster and easier to set up, making it a popular choice for quick linting tasks. pylint is often preferred for more thorough code analysis and when a project requires stricter adherence to coding standards.
An extremely fast Python linter and code formatter, written in Rust.
Pros of Ruff
- Significantly faster performance due to Rust implementation
- Combines functionality of multiple tools (Flake8, Black, isort, etc.)
- Automatic code fixes for many issues
Cons of Ruff
- Newer project with potentially less stability
- May not support all Flake8 plugins or custom rules
- Learning curve for users familiar with Flake8 ecosystem
Code Comparison
Flake8 configuration:
[flake8]
max-line-length = 88
extend-ignore = E203, E266, E501, W503
Ruff configuration:
[tool.ruff]
line-length = 88
select = ["E", "F", "W"]
ignore = ["E203", "E266", "E501", "W503"]
Both tools can be configured to follow similar rules, but Ruff's TOML-based configuration offers more flexibility and readability. Ruff also provides additional features like automatic code fixing, which Flake8 does not offer out of the box.
While Flake8 has been the go-to linter for Python projects for years, Ruff is gaining popularity due to its speed and comprehensive feature set. However, Flake8's maturity and extensive plugin ecosystem still make it a reliable choice for many projects.
Simple Python style checker in one Python file
Pros of pycodestyle
- Focused solely on PEP 8 style checking
- Lightweight and fast execution
- Can be used as a standalone tool or integrated into other projects
Cons of pycodestyle
- Limited scope compared to flake8's broader functionality
- Fewer configuration options and less flexibility
- Doesn't include additional plugins or extensions
Code Comparison
pycodestyle:
import pycodestyle
style_guide = pycodestyle.StyleGuide()
result = style_guide.check_files(['example.py'])
print(result.total_errors)
flake8:
import flake8.api.legacy as flake8
style_guide = flake8.get_style_guide()
result = style_guide.check_files(['example.py'])
print(result.total_errors)
The code examples show that both tools can be used to check Python files for style issues. However, flake8 provides a more comprehensive analysis, including additional checks beyond PEP 8 style guidelines. pycodestyle is more focused on PEP 8 compliance, while flake8 offers a broader range of linting capabilities and can be extended with plugins for additional functionality.
docstring style checker
Pros of pydocstyle
- Focused specifically on docstring style and conventions
- Supports various docstring styles (e.g., Google, NumPy, Sphinx)
- Can be easily integrated with other linting tools
Cons of pydocstyle
- Limited scope compared to flake8's broader code style checks
- May require additional configuration for project-specific docstring conventions
- Less frequent updates and smaller community compared to flake8
Code Comparison
pydocstyle:
def example_function():
"""
This is a docstring.
Returns:
None
"""
pass
flake8:
def example_function():
# flake8 checks for code style, not docstrings
x = 1
y = 2
return x + y
pydocstyle focuses on docstring conventions, while flake8 covers a broader range of code style issues. pydocstyle is ideal for projects emphasizing consistent documentation, whereas flake8 is better suited for general code quality and style enforcement. Both tools can be used together for comprehensive Python code analysis.
A formatter for Python files
Pros of YAPF
- Automatically reformats code to adhere to style guidelines
- Offers more extensive formatting options and customization
- Can handle complex code structures and maintain readability
Cons of YAPF
- May alter code structure more aggressively, potentially changing behavior
- Slower performance compared to Flake8, especially on large codebases
- Learning curve for configuration and style options
Code Comparison
Flake8 (linting):
def example_function(param1, param2):
result = param1 + param2
return result
YAPF (formatting):
def example_function(param1,
param2):
result = param1 + param2
return result
Key Differences
- Flake8 focuses on identifying style and syntax issues without modifying code
- YAPF actively reformats code to comply with style guidelines
- Flake8 is primarily a linter, while YAPF is a code formatter
- Flake8 is generally faster and less intrusive in its operation
- YAPF provides more comprehensive formatting capabilities but may require more setup
Use Cases
- Use Flake8 for quick style checks and identifying potential errors
- Use YAPF for enforcing consistent code style across a project or team
- Consider using both tools in combination for comprehensive code quality management
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
.. image:: https://github.com/PyCQA/flake8/workflows/main/badge.svg :target: https://github.com/PyCQA/flake8/actions?query=workflow%3Amain :alt: build status
.. image:: https://results.pre-commit.ci/badge/github/PyCQA/flake8/main.svg :target: https://results.pre-commit.ci/latest/github/PyCQA/flake8/main :alt: pre-commit.ci status
.. image:: https://img.shields.io/discord/825463413634891776.svg :target: https://discord.gg/qYxpadCgkx :alt: Discord
======== Flake8
Flake8 is a wrapper around these tools:
- PyFlakes
- pycodestyle
- Ned Batchelder's McCabe script
Flake8 runs all the tools by launching the single flake8
command.
It displays the warnings in a per-file, merged output.
It also adds a few features:
-
files that contain this line are skipped::
flake8: noqa
-
lines that contain a
# noqa
comment at the end will not issue warnings. -
you can ignore specific errors on a line with
# noqa: <error>
, e.g.,# noqa: E234
. Multiple codes can be given, separated by comma. Thenoqa
token is case insensitive, the colon before the list of codes is required otherwise the part afternoqa
is ignored -
Git and Mercurial hooks
-
extendable through
flake8.extension
andflake8.formatting
entry points
Quickstart
See our quickstart documentation <https://flake8.pycqa.org/en/latest/index.html#quickstart>
_ for how to install
and get started with Flake8.
Frequently Asked Questions
Flake8 maintains an FAQ <https://flake8.pycqa.org/en/latest/faq.html>
_ in its
documentation.
Questions or Feedback
If you have questions you'd like to ask the developers, or feedback you'd like to provide, feel free to use the mailing list: code-quality@python.org
We would love to hear from you. Additionally, if you have a feature you'd like to suggest, the mailing list would be the best place for it.
Links
-
Flake8 Documentation <https://flake8.pycqa.org/en/latest/>
_ -
GitHub Project <https://github.com/pycqa/flake8>
_ -
All (Open and Closed) Issues <https://github.com/pycqa/flake8/issues?q=is%3Aissue>
_ -
Code-Quality Archives <https://mail.python.org/mailman/listinfo/code-quality>
_ -
Code of Conduct <https://flake8.pycqa.org/en/latest/internal/contributing.html#code-of-conduct>
_ -
Getting Started Contributing <https://flake8.pycqa.org/en/latest/internal/contributing.html>
_
Maintenance
Flake8 was created by Tarek Ziadé and is currently maintained by anthony sottile <https://github.com/sponsors/asottile>
_ and Ian Cordasco <https://www.coglib.com/~icordasc/>
_
Top Related Projects
The uncompromising Python code formatter
It's not just a linter that annoys you!
An extremely fast Python linter and code formatter, written in Rust.
Simple Python style checker in one Python file
docstring style checker
A formatter for Python files
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot