Top Related Projects
An extremely fast Python linter and code formatter, written in Rust.
The uncompromising Python code formatter
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.
It's not just a linter that annoys you!
A formatter for Python files
A tool that automatically formats Python code to conform to the PEP 8 style guide.
Quick Overview
Ruff-pre-commit is a pre-commit hook for Ruff, an extremely fast Python linter written in Rust. This repository provides configuration and setup files to easily integrate Ruff into your pre-commit workflow, ensuring code quality checks are performed automatically before commits.
Pros
- Seamless integration with pre-commit, a popular Git hook management tool
- Leverages Ruff's speed for faster linting in pre-commit checks
- Easy to set up and configure
- Helps maintain consistent code quality across a project
Cons
- Requires pre-commit to be installed and set up in the project
- May add a slight delay to the commit process, especially for larger codebases
- Limited to Python projects only
- Depends on Ruff's development and maintenance
Getting Started
To use ruff-pre-commit in your project:
-
Install pre-commit:
pip install pre-commit
-
Add the following to your
.pre-commit-config.yaml
:repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.0.270 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix]
-
Install the pre-commit hook:
pre-commit install
Now, Ruff will automatically run as a pre-commit hook when you attempt to commit changes to your Python project.
Competitor Comparisons
An extremely fast Python linter and code formatter, written in Rust.
Pros of Ruff
- More comprehensive Python linting and formatting tool
- Faster execution due to being written in Rust
- Wider range of features and customization options
Cons of Ruff
- Requires separate installation and configuration
- May have a steeper learning curve for new users
- Potentially more complex setup for project-specific rules
Code Comparison
Ruff configuration example:
[tool.ruff]
select = ["E", "F", "I"]
ignore = ["E501"]
line-length = 88
Ruff-pre-commit configuration example:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
hooks:
- id: ruff
args: [--fix]
Summary
Ruff is a standalone Python linter and formatter, offering more features and flexibility. Ruff-pre-commit is specifically designed for use with pre-commit hooks, providing an easier integration into existing workflows. While Ruff offers more comprehensive linting capabilities, Ruff-pre-commit simplifies the setup process for pre-commit environments. The choice between the two depends on project requirements and the desired level of customization.
The uncompromising Python code formatter
Pros of Black
- Widely adopted and considered the standard for Python code formatting
- Deterministic output, ensuring consistent formatting across different environments
- Extensive configuration options for fine-tuning formatting behavior
Cons of Black
- Slower performance compared to Ruff, especially on larger codebases
- Less comprehensive linting capabilities, focusing primarily on code formatting
- Requires a separate tool for additional linting features
Code Comparison
Black:
def example_function(arg1, arg2,arg3):
result = arg1 + arg2 + arg3
return result
Ruff:
def example_function(arg1, arg2, arg3):
result = arg1 + arg2 + arg3
return result
Both tools would format the code similarly, removing extra spaces and ensuring consistent indentation. However, Ruff can perform additional linting checks beyond formatting.
Key Differences
- Ruff is significantly faster than Black, especially for large projects
- Ruff combines formatting and linting in a single tool, while Black focuses solely on formatting
- Black has a longer history and wider adoption in the Python community
- Ruff offers more comprehensive code analysis and error detection capabilities
While Black remains a popular choice for Python code formatting, Ruff provides a faster alternative with additional linting features, making it an attractive option for developers seeking improved performance and broader code quality checks.
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.
Pros of Flake8
- Well-established and widely adopted in the Python community
- Extensive plugin ecosystem for customization
- Supports a broad range of Python versions
Cons of Flake8
- Slower performance compared to Ruff
- Limited auto-fix capabilities
- Requires separate installation of plugins
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 enforce similar coding standards, but Ruff offers a more concise configuration format and includes additional features like auto-fixing by default.
Ruff is designed as a faster alternative to Flake8, with built-in support for many popular linting rules. It aims to provide a more comprehensive and efficient linting experience out of the box, while Flake8 relies on its plugin ecosystem for extended functionality.
It's not just a linter that annoys you!
Pros of Pylint
- More comprehensive and customizable linting options
- Longer history and established community support
- Provides in-depth code analysis beyond just style checks
Cons of Pylint
- Slower performance, especially on larger codebases
- Can produce more false positives, requiring configuration tweaks
- Steeper learning curve for advanced usage
Code Comparison
Pylint configuration example:
[MASTER]
ignore=CVS
ignore-patterns=
persistent=yes
load-plugins=
jobs=1
unsafe-load-any-extension=no
extension-pkg-whitelist=
Ruff configuration example:
[tool.ruff]
line-length = 88
select = ["E", "F", "I"]
ignore = ["E501"]
Pylint offers more granular control but requires more setup, while Ruff provides a simpler configuration with faster execution. Ruff is designed specifically for pre-commit hooks, making it more streamlined for that use case. However, Pylint's broader feature set allows for more thorough code analysis beyond just style checks.
Both tools aim to improve code quality, but they cater to different needs. Pylint is better suited for projects requiring deep, customizable analysis, while Ruff excels in quick, efficient linting, especially in pre-commit scenarios.
A formatter for Python files
Pros of YAPF
- More mature and established project with a longer history
- Offers more customization options for code formatting
- Supports a wider range of Python versions (2.7 and 3.6+)
Cons of YAPF
- Slower performance compared to Ruff
- Less frequent updates and maintenance
- Focuses solely on code formatting, while Ruff offers additional linting features
Code Comparison
YAPF configuration example:
[style]
based_on_style = pep8
spaces_before_comment = 4
split_before_logical_operator = true
Ruff configuration example:
[tool.ruff]
line-length = 88
select = ["E", "F", "I"]
ignore = ["E501"]
Summary
YAPF is a more established code formatter with extensive customization options, while Ruff is a newer, faster alternative that combines formatting with linting capabilities. YAPF supports older Python versions, but Ruff offers better performance and more frequent updates. The choice between the two depends on specific project requirements, such as Python version support, formatting preferences, and the need for additional linting features.
A tool that automatically formats Python code to conform to the PEP 8 style guide.
Pros of autopep8
- Well-established and widely used in the Python community
- Offers fine-grained control over formatting options
- Can be used as a standalone tool or integrated into various editors
Cons of autopep8
- Slower performance compared to Ruff
- Limited to PEP 8 style guide enforcement
- Requires separate tools for additional linting and code quality checks
Code Comparison
autopep8:
from autopep8 import fix_code
code = '''
def foo():
x = 1
y = 2
return x+y
'''
fixed_code = fix_code(code)
Ruff:
from ruff import fix
code = '''
def foo():
x = 1
y = 2
return x+y
'''
fixed_code = fix(code)
While both tools aim to improve code quality, Ruff offers a more comprehensive and faster solution for Python code formatting and linting. autopep8 focuses primarily on PEP 8 compliance, while Ruff combines multiple linting and formatting tools into a single, efficient package. The code comparison shows that both tools can be used programmatically, but Ruff's API is designed to be more straightforward and performant.
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
ruff-pre-commit
A pre-commit hook for Ruff.
Distributed as a standalone repository to enable installing Ruff via prebuilt wheels from PyPI.
Using Ruff with pre-commit
To run Ruff's linter and formatter
(available as of Ruff v0.0.289) via pre-commit, add the following to your .pre-commit-config.yaml
:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.4
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
To enable lint fixes, add the --fix
argument to the lint hook:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.4
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
To avoid running on Jupyter Notebooks, remove jupyter
from the list of allowed filetypes:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.4
hooks:
# Run the linter.
- id: ruff
types_or: [ python, pyi ]
args: [ --fix ]
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi ]
When running with --fix
, Ruff's lint hook should be placed before Ruff's formatter hook, and
before Black, isort, and other formatting tools, as Ruff's fix behavior can output code changes
that require reformatting.
When running without --fix
, Ruff's formatter hook can be placed before or after Ruff's lint hook.
(As long as your Ruff configuration avoids any linter-formatter incompatibilities,
ruff format
should never introduce new lint errors, so it's safe to run Ruff's format hook after
ruff check --fix
.)
License
ruff-pre-commit is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ruff-pre-commit by you, as defined in the Apache-2.0 license, shall be dually licensed as above, without any additional terms or conditions.
Top Related Projects
An extremely fast Python linter and code formatter, written in Rust.
The uncompromising Python code formatter
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.
It's not just a linter that annoys you!
A formatter for Python files
A tool that automatically formats Python code to conform to the PEP 8 style guide.
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