A lint running tool and framework.
Project description
Lintrunner
Overview
lintrunner
is a tool that runs linters. It is responsible for:
- Deciding which files need to be linted.
- Invoking linters according to a common protocol.
- Gathering results and presenting them to users.
The intention is to provide a universal way to configure and invoke linters, which is useful on large polyglot projects.
The design of lintrunner
is heavily inspired by linttool
, a project that exists internally at Meta.
Installation
pip install lintrunner
Usage
First, you need to add a configuration file to your repo. See the Linter configuration section for more info.
Then, simply run lintrunner
to lint your changes!
How to control what paths to lint lintrunner
When run with no arguments, lintrunner
will check:
- The files changed in the
HEAD
commit. - The files changed in the user’s working tree.
It does not check:
- Any files not tracked by
git
;git add
them to lint them.
There are multiple ways to customize how paths are checked:
Pass paths as positional arguments
For example:
lintrunner foo.py bar.cpp
This naturally composes with xargs
, for example the canonical way to check
every path in the repo is:
git grep -Il . | xargs lintrunner
--configs
/ --config
"Comma-separated paths to lintrunner configuration files.
Multiple files are merged, with later definitions overriding earlier ones.
ONLY THE FIRST is required to be present on your machine.
Defaults to lintrunner.toml, lintrunner.private.toml
. Extra configs like lintrunner.private.toml
are useful for combining project-wide and local configs."
--paths-cmd
Some ways to invoke xargs
will cause multiple lintrunner
processes to be
run, increasing lint time (especially on huge path sets). As an alternative that
gives lintrunner
control of parallelization, you can use --paths-cmd
. If
--paths-cmd
is specified lintrunner
will execute that command and consider
each line of its stdout
to be a file to lint.
For example, the same command above would be:
lintrunner --paths-cmd='git grep -Il .'
--paths-file
If this is specified, lintrunner
will read paths from the given file, one per
line, and check those. This can be useful if you have some really complex logic
to determine which paths to check.
--revision
This value can be any <tree-ish>
accepted by git diff-tree
, like a commit
hash or revspec. If this is specified, lintrunner
will check:
- All paths changed from
<tree-ish>
toHEAD
- All paths changed in the user's working tree.
--merge-base-with
Like --revision
, except the revision is determined by computing the merge-base
of HEAD
and the provided <tree-ish>
. This is useful for linting all commits
in a specific pull request. For example, for a pull request targeting master,
you can run:
lintrunner -m master
--all-files
This will run lint on all files specified in .lintrunner.toml
.
--only-lint-under-config-dir
If set, will only lint files under the directory where the configuration file is located and its subdirectories.
Linter configuration
lintrunner
knows which linters to run and how by looking at a configuration
file, conventionally named .lintrunner.toml
.
Here is an example linter configuration:
merge_base_with = 'main'
[[linter]]
name = 'FLAKE8'
include_patterns = [
'src/**/*.py', # unix-style globs supported
'test/**/*.py',
]
exclude_patterns = ['src/my_bad_file.py']
command = [
'python3',
'flake8_linter.py',
'—-',
# {{PATHSFILE}} gets rewritten to a tmpfile containing all paths to lint
'@{{PATHSFILE}}',
]
A complete description of the configuration schema can be found here.
Linter protocol
Most linters have their own output format and arguments. In order to impose
consistency on linter invocation and outputs, lintrunner
implements a protocol
that it expects linters to fulfill. In most cases, a small script (called a
linter adapter) is required to implement the protocol for a given external
linter. You can see some example adapters in examples/
.
Invocation
Linters will be invoked according to the command
specified by their
configuration. They will be called once per lint run.
If a linter needs to know which paths to run on, it should take a
{{PATHSFILE}}
argument. During invocation, the string {{PATHSFILE}}
will be
replaced with the name of a temporary file containing which paths the linter
should run on, one path per line.
A common way to implement this in a linter adapter is to use argparse
’s
fromfile_prefix_chars
feature. In the Flake8 example above, we use @
as the fromfile_prefix_chars
argument, so argparse
will automatically read the {{PATHSFILE}}
and supply
its contents as a list of arguments.
Output
Any lint messages a linter would like to communicate the user must be
represented as a LintMessage
. The linter, must print LintMessage
s as JSON
Lines to stdout
, one message per line. Output to
stderr
will be ignored.
A complete description of the LintMessage schema can be found here.
Exiting
Linters should always exit with code 0. This is true even if lint errors are
reported; lintrunner
itself will determine how to exit based on what linters
report.
To signal a general linter failure (which should ideally never happen!), linters
can return a LintMessage
with path = None
.
In the event a linter exits non-zero, it will be caught by lintrunner
and
presented as a “general linter failure” with stdout/stderr shown to the user.
This should be considered a bug in the linter’s implementation of this protocol.
Tips for adopting lintrunner
in a new project
When adopting lintrunner in a previously un-linted project, it may generate a lot
of lint messages. You can use the --output oneline
option to make
lintrunner
display each lint message in its separate line to quickly navigate
through them.
Additionally, you can selectively run specific linters with the --take
option,
like --take RUFF,CLANGFORMAT
, to focus on resolving specific lint errors, or
use --skip
to skip a long running linter like MYPY
.
GitHub Action
To use lintrunner
in a GitHub workflow, you can consider lintrunner-action
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file lintrunner-0.12.5.tar.gz
.
File metadata
- Download URL: lintrunner-0.12.5.tar.gz
- Upload date:
- Size: 62.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35b7a9895fa39280a0ef5006c454167f357b7851b0410c1edb06a1da7956ba3f |
|
MD5 | 5cf041201952d6ae9c29e524b54864d6 |
|
BLAKE2b-256 | 465bddaaf36680e92241d0e0f6e7c76fb12a9657b9791c2d3cb1f5111de24fdf |
File details
Details for the file lintrunner-0.12.5-py3-none-win32.whl
.
File metadata
- Download URL: lintrunner-0.12.5-py3-none-win32.whl
- Upload date:
- Size: 1.6 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7263fdce8bf1dc31ebcec6c35786f33346b01b0bba2c4e195898d1563c376f2 |
|
MD5 | 21b625b771d53f6c2f8190cfa93d3e21 |
|
BLAKE2b-256 | c4705d3c5dfef3ac70e72ea7898f69716f43610175a9c82e202e8df63427a09e |
File details
Details for the file lintrunner-0.12.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: lintrunner-0.12.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.9 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba58bbca25a3ad0cdcdf9250ca152ee686f92dfba4ce3ecf05e0c090616ca0b8 |
|
MD5 | 4654c56f031b0d3b89f1a9fb59d3b97c |
|
BLAKE2b-256 | a269eeab499426cfa1c2ab02a093b9d6f35008dbaab07e9dc157d67c0997644e |
File details
Details for the file lintrunner-0.12.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: lintrunner-0.12.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 2.9 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad6b0b1b7606562b82ba3210eb90b4db8f8f8b9ca97aa19d37d4a05866d0980c |
|
MD5 | c52bdce8fd9ff83b8ff44993489c6880 |
|
BLAKE2b-256 | bc208bde31193655aa4ab671fb8f54b485cbb34148e42811be54354b3284e33c |
File details
Details for the file lintrunner-0.12.5-py3-none-macosx_11_0_arm64.whl
.
File metadata
- Download URL: lintrunner-0.12.5-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.9 MB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd346b2e65470d0af3f49428e21f0c9c3dbc56ad40e8234516b5722d19881a13 |
|
MD5 | dd360927635ddfc0380ff8a52bb00bf5 |
|
BLAKE2b-256 | d1a6e301de25127e2cb6a57d81d5f510f37136743d5a6d2ca228f6e539a4cd7d |
File details
Details for the file lintrunner-0.12.5-py3-none-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: lintrunner-0.12.5-py3-none-macosx_10_7_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: Python 3, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d790820422dd424c54ec6a0128e4eaf9269b1f6602ddcc53f124a629d73202bf |
|
MD5 | 234524f5b046a6f67d4a8be7415faab0 |
|
BLAKE2b-256 | 33a1391551d5dd2adb3347e57b6f6c45be9a6a583234381099633b7a6c81be03 |