tukuroi: linting framework with auto-fixes¶
tukuroi is an unofficial fork of Instagram/Fixit, the linting framework that Meta published. It is not affiliated with Meta, nor endorsed by Meta or by the maintainers of Fixit. Its main differences from upstream:
The rules run on a new engine,
tukuroi.core, built on Python’sastmodule in place of LibCST.Safer handling of untrusted repositories:
--no-custom-ruleskeeps tukuroi from importing any rules but its built-in ones, the search for configurations stops at the root of the repository, output templates take only the names of their fields, and the LSP server talks over stdio only.It supports only the two newest minor versions of CPython: 3.14 and 3.15.
tukuroi provides a highly configurable linting framework with support for
auto-fixes, custom “local” lint rules, and hierarchical configuration, built
on Python’s ast module. A fix replaces only the code that it fixes; a
project can choose to format the fixed files with
ruff (pip install "tukuroi[ruff]").
tukuroi makes it quick and easy to write new lint rules and offer suggested changes for any errors found, which can then be accepted automatically, or presented to the user for consideration.
For more details, see the user guide.
Quick Start¶
Setup¶
Install tukuroi from PyPI:
$ pip install tukuroi
By default, tukuroi enables all of the lint rules that ship with tukuroi,
all of which are part of the tukuroi.rules package.
If you want to customize the list of enabled rules, either to add new rules or disable others, see the Configuration Guide for details and options available.
tukuroi offers multiple integrations, including pre-commit actions and LSP support for VSCode and other editors. See the Integrations Guide for details.
Usage¶
See lints and suggested changes for a set of source files:
$ tukuroi lint <path>
Apply suggested changes on those same files automatically:
$ tukuroi fix <path>
If given directories, tukuroi will automatically recurse them, finding any files
with the .py or .pyi extension, while obeying the .gitignore at the
root of your repo.
See the Command Reference for more details.
Example¶
Given the following code:
# foo_test.py
import unittest
class FooTest(unittest.TestCase):
def test_bar(self) -> None:
self.assertTrue(value in values)
if True:
self.assertTrue(value, None)
When running tukuroi, we see three separate lint errors:
$ tukuroi lint foo_test.py
foo_test.py@8:8 UseAssertIn: Use assertIn/assertNotIn instead of assertTrue/assertFalse for inclusion check.
See https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIn) (has autofix)
foo_test.py@9:8 NoStaticIfCondition: Your if condition appears to evaluate to a static value (e.g. `or True`, `and False`). Please double check this logic and if it is actually temporary debug code.
foo_test.py@10:12 NoAssertTrueForComparisons: "assertTrue" does not compare its arguments, use "assertEqual" or other appropriate functions. (has autofix)
🛠️ 1 file checked, 1 file with errors, 2 auto-fixes available 🛠️
We can also see any suggested changes by passing --diff:
$ tukuroi lint --diff foo_test.py
foo_test.py@8:8 UseAssertIn: Use assertIn/assertNotIn instead of assertTrue/assertFalse for inclusion check.
See https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIn) (has autofix)
--- a/foo_test.py
+++ b/foo_test.py
@@ -7,3 +7,3 @@
def test_bar(self) -> None:
- self.assertTrue(value in values)
+ self.assertIn(value, values)
if True:
...
tukuroi checks the rules that come with it (see Built-in Rules); ruff covers the others that Fixit had (see Rules Replaced by ruff). To write rules of your own, see Writing Rules.
Silencing Errors¶
For lint rules without autofixes, it may still be useful to silence individual
errors. A simple # lint-ignore or # lint-fixme comment, either as
a trailing inline comment, or as a dedicated comment line above the code that
triggered the lint rule:
self.assertTrue(a in b) # lint-fixme: UseAssertIn
# lint-ignore: UseAssertIn
self.assertTrue(c in d)
By providing one or more lint rule, separated by commas, tukuroi can still report
issues triggered by other lint rules that haven’t been listed in the comment,
but this is not required. The names follow a :, and must have the same
case as the names of the rules, with or without Rule at the end. A
directive that does not fit this form silences nothing, and tukuroi warns about
it (invalid lint-ignore directive). A comment can hold other tools’
directives too: # noqa # lint-ignore: UseAssertIn.
If no rule name is listed, tukuroi will silence all rules when reported on code associated with that comment:
self.assertTrue(a in b) # lint-ignore
“ignore” vs “fixme”¶
Both comment directives achieve the same result — silencing errors for a particular statement of code. The semantics of using either term is left to the user, though they are intended to be used with the following meanings:
# lint-fixmefor errors that need to be corrected or reviewed at a later date, but where the lint rule should be silenced temporarily for the sake of CI or similar external circumstances.# lint-ignorefor errors that are false-positives (please report issues if this occurs with built-in lint rules) or the code is otherwise intentionally written or structured in a way that the lint error cannot be avoided.
Future versions of tukuroi may offer reporting or similar tools that treat “fixme” directives differently from “ignore” directives.
Coming from Fixit¶
tukuroi reads none of the names of Fixit. Rename them:
Fixit |
tukuroi |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
the pre-commit hooks |
|
|
|
|
|
the LSP server |
|
A config of Fixit that tukuroi does not read is a warning: a fixit.toml, a
.fixit.toml, or a [tool.fixit] in pyproject.toml, in a directory
with no config of tukuroi. To keep one for Fixit and run tukuroi with its
defaults, put an empty [tool.tukuroi] (or an empty tukuroi.toml) next to
it.
The settings of Fixit that tukuroi does not support (python-version,
options, tags) are configuration errors that say so. formatter takes
"ruff" or "none" only, not "black" or "ufmt".
The rules of Fixit do not run on tukuroi: they are written for LibCST, and
tukuroi uses ast. To port a rule:
Fixit |
tukuroi |
|---|---|
a subclass of |
a subclass of |
|
|
a LibCST node as the |
a string as the |
|
|
|
|
See Writing Rules for the details.
License¶
tukuroi is MIT licensed, as found in the LICENSE file.