Lintrunner Adapters
Lintrunner Adapters provides a collection of wrappers and tools that integrate various linters (like Black, Ruff, Pylint, Bandit, etc.) with the `lintrunner` framework. It enables unified linting across a project using a single configuration. The current version is 0.13.0, and releases are frequent, primarily adding new linter adapters or fixing issues with existing ones.
Warnings
- gotcha The `lintrunner-adapters` library itself does not install the underlying linters (e.g., `ruff`, `black`, `pylint`). You must explicitly install each linter that you intend to use via its respective adapter.
- gotcha Breaking changes in underlying linters (e.g., `ruff`, `black`) can cause adapters to behave unexpectedly or fail. `lintrunner-adapters` updates are typically released to maintain compatibility with new linter versions.
- breaking The `PylintLinter` adapter changed its behavior to raise an error when Pylint fails, instead of failing silently. Scripts or CI pipelines that relied on silent failures might break.
- gotcha This library requires Python 3.9 or newer. Running with older Python versions will result in installation or runtime errors.
Install
-
pip install lintrunner-adapters
Imports
- RuffLinter
from lintrunner_adapters.ruff_linter import RuffLinter
- BlackLinter
from lintrunner_adapters.black_linter import BlackLinter
Quickstart
import os
# 1. Create a sample Python file to lint
python_code = """def foo ( x ):\n return x+1\n"""
with open("test_file.py", "w") as f:
f.write(python_code)
# 2. Create a lintrunner.toml configuration (simulating user setup)
# In a real project, this would be a file named `lintrunner.toml`
lintrunner_toml_content = """
[linter.ruff]
linter_class = "lintrunner_adapters.ruff_linter:RuffLinter"
[linter.black]
linter_class = "lintrunner_adapters.black_linter:BlackLinter"
args = ["--check", "--diff"]
"""
with open("lintrunner.toml", "w") as f:
f.write(lintrunner_toml_content)
# 3. Ensure lintrunner and the linters are installed for this example to run
# In a real setup, `pip install lintrunner ruff black` would be done.
# We can't actually run lintrunner directly in this snippet, but demonstrate setup.
print("Created test_file.py and lintrunner.toml. To run, execute:")
print("pip install lintrunner ruff black")
print("lintrunner --config lintrunner.toml test_file.py")
# Clean up (optional, for standalone execution)
# os.remove("test_file.py")
# os.remove("lintrunner.toml")