brash
Codemods that hold a Python codebase to its conventions. A linter can only report that a module constant is not
Final or that a parameter is typed list where Iterable would do; brash rewrites the file. It is built on
LibCST, which keeps every comment and blank line a rewrite does not touch.
brash is meant to run twice in a project, as a formatter does: once to apply (brash ., in the step that formats)
and once to check (brash --check ., in the lint), so the repository stays as the codemods leave it and a change that
drifts fails CI with the diff that fixes it. It came out of canopy, whose own
code is held to it.
Install
brash needs Python 3.12 or later. Add it to a project as a dev dependency, or run it without installing:
uv add --dev brash # or: pip install brash
uvx brash --check . # run it once, installing nothing
Usage
brash . # rewrite every Python file under the directory
brash --check . # change nothing: print the diff, exit 1 if there is one
brash --only final-constants src/app.py # one codemod, one file; give --only again for each
brash --list # the codemods, brash's own and the project's
A directory gives its .py files and its scripts that open with a Python shebang, but nothing under a hidden
directory, node_modules, site-packages, a venv, or a build's output. A file named outright is taken as it is,
whatever its name, which is how a git hook runs brash on a commit's files. Neither gives a file the project
excludes.
The exit code is 0 when there is nothing to report, 1 when --check found a file a codemod would rewrite or a file
could not be rewritten (a syntax error, say, reported by name), and 2 for a usage error.
Run brash before the formatter: a codemod adds an import where LibCST puts it and writes code unformatted, and
ruff check --select I --fix and ruff format (or isort and black) then put both right. In a Makefile:
fmt:
uv run brash .
uv run ruff check --select I --fix .
uv run ruff format .
lint:
uv run ruff format --check .
uv run ruff check .
uv run brash --check .
Every codemod runs by default but one that says it is off by default, which of brash's own is explicit-override,
for a convention few projects hold. A project turns one off, or names the few it wants, with ignore, select and
extend-select in its configuration; --only names the ones for a single run, and is taken in
place of them. Its per-file-ignores there turns one off for some files alone.
A project whose code runs on a Python older than 3.12 sets its target-version, and a codemod that writes syntax
that Python does not have, which is type-aliases or explicit-override, does not run; --target-version py310
says it for a single run.
Configuration
A project configures brash in its pyproject.toml, under [tool.brash]. Every setting is optional:
[tool.brash]
target-version = "py310"
ignore = ["general-params"]
exclude = ["src/app/_generated", "*_pb2.py"]
[tool.brash.per-file-ignores]
"tests/*" = ["general-params"]
[tool.brash.codemods]
no-print = "codemods.no_print:NoPrint"
selectis an allowlist: the codemods that run, by name, and no other. Left out, every codemod runs but one off by default, so a release of brash that adds one runs it; withselect, a new one waits to be named.extend-selectnames codemods that run as well: added to those ofselect, or, where there is noselect, to those that run by default. It is how a project turns on a codemod that is off by default, and still runs each one a release of brash adds.ignoreis a blocklist: the codemods that do not run. With the others, a run has the codemods ofselectandextend-selectthatignoredoes not name. Each takes your project's own codemods as it takes brash's, and a name no codemod has fails the run. The order codemods run in is brash's, whatever the list's.--onlyon the command line is taken in place of all three, so it runs an ignored codemod, and--listlists every codemod, on or off, and says which are off by default.per-file-ignoresturns codemods off for some files alone: each key is a glob pattern, asexclude's are and matching what one would, and its value the codemods that do not run on the files it matches. A file that several patterns match is rewritten by none of the codemods any of them names. It holds for a file named outright, and for a codemod--onlynames:--onlyruns it on the project's other files. A name no codemod has fails the run.target-versionis the oldest Python the project's code runs on, as ruff spells it:py310for 3.10. A codemod whose output needs a later Python does not run, named by--onlyor not. Left out, every codemod runs, whatever the project'srequires-python: brash does not read it.--target-versionon the command line is taken in place of it.excludelists the files and directories no codemod rewrites, such as generated code, as glob patterns of paths relative to the directory that holds thepyproject.toml. A pattern excludes the file or directory it matches, and everything under that directory. Its*matches any run of characters, slashes among them, so*_pb2.pyexcludes such a file at any depth andtop.pyonly the one beside thepyproject.toml. An excluded file is left alone when it is named outright too, as a git hook names it.extend-genericsnames generic typestype-aliasestakes as it takes the standard library's: your project's own, and those of the libraries it uses. Each is the type's module and its name there, asextend-generics = ["numpy.typing.NDArray"], and it is taken in a module that imports it from there: by name, under another name, or throughimport numpy as npasnp.typing.NDArray. brash reads the module's imports and no other file, so a type that reaches the module by another path, re-exported from your own package, is named under that path too, and one a*brings is left alone. A relative import gives only the end of a path, sofrom .tables import Tableisapp.tables.Table. A type the module defines itself is not imported, and an alias of it there is left alone. A name with no module fails the run.codemodsregisters your project's own codemods.
A project that keeps each tool's settings in a file of its own has a brash.toml instead, with the same settings at
its top level, and no [tool.brash] above them:
target-version = "py310"
ignore = ["general-params"]
exclude = ["src/app/_generated", "*_pb2.py"]
[per-file-ignores]
"tests/*" = ["general-params"]
[codemods]
no-print = "codemods.no_print:NoPrint"
brash reads the nearest brash.toml or pyproject.toml, in the directory it runs from or one above it, and the
brash.toml where a directory holds both: the [tool.brash] beside it is not read. What is said here of the
pyproject.toml, such as what exclude's patterns are relative to, is as true of a brash.toml. A setting it does
not have, or a value of the wrong type, fails the run with exit code 1 and a line for each, naming the file and the
key:
Error: /repo/pyproject.toml: tool.brash.excludes: no such setting; there are `codemods`, `select`, `extend-select`, `ignore`, `per-file-ignores`, `target-version`, `exclude`
As a git hook
brash is a hook for pre-commit and prek, which read the same
.pre-commit-config.yaml. List it before the formatter's hooks, for the reason it runs before the formatter:
repos:
- repo: https://github.com/eddieland/brash
rev: v0.2.0 # the latest release
hooks:
- id: brash
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.5
hooks:
- id: ruff-check
args: [--select, I, --fix]
- id: ruff-format
brash rewrites the commit's Python files, and the runner stops the commit where one changed, for you to stage.
brash-check changes nothing and fails with the diff, for a run in CI. Either takes args: [--only, final-constants].
In prek, a hook given a priority runs alongside the others that share it: give brash a lower one than the formatter.
The hook runs in an environment of its own, which holds brash and nothing of your project:
-
Your project's codemods run too, read from the working tree as the command line reads them. One that imports a package other than
libcstandbrashnames it in the hook'sadditional_dependencies. One that imports your project as installed needs your environment, which is a local hook:repos: - repo: local hooks: - id: brash name: brash entry: uv run brash language: system types: [python] require_serial: true
-
The
pyproject.tomlis the one at the repository's root, where a hook runs, so codemods registered in one below it, inbackend/pyproject.toml, are not run. prek runs a hook from the directory of the configuration that names it: givebackend/a.pre-commit-config.yamlof its own. For pre-commit, make the local hook's entrybash -c 'cd backend && uv run brash "${@#backend/}"' --, withfiles: ^backend/. -
It needs Python 3.12 or later: where the runner's default is older, set the hook's
language_version.
The codemods
final-constantsannotates every module constantFinal, so the type checker refuses a second assignment:LIMIT = 3becomesLIMIT: Final = 3, andNAMES: Sequence[str] = ()becomesNAMES: Final[Sequence[str]] = (). A constant is anUPPER_CASEname bound once, by an assignment among the module's own statements. It leaves alone a lowercase name, which is how a module spells an object with state of its own (_log,router); a name bound again anywhere in the module (a second assignment,+=,del, a function'sglobal); an object made to hold state whatever its name, such as a logger or a lock; and aTypeVaror an explicitTypeAlias.type-aliasesspells an implicit type alias as atypestatement, which ruff does only for one annotatedTypeAlias:Outcome = Literal["packed", "failed"]becomestype Outcome = Literal["packed", "failed"]. An alias is aCamelCasename bound once, among the module's own statements, to aLiteral, a generic type of the standard library that holds types (dict[str, Row],tuple[str, int],Callable[[str], Reply],Sequence[Row], and the namestypinghas for them), or a union of types, spelledA | B,Union[A, B], orOptional[A]: ruff reports the last two and fixes neither in an assignment, and writes the|once the alias is atypestatement. Atypestatement makes aTypeAliasType, not the type it names, so it leaves alone a name the module reads outside an annotation (passed toget_argsorisinstance, called, subclassed, held in another assignment); anAnnotated[...]alias, whose metadata frameworks read as the program runs; a subscript of anything else that is not the standard library's (Table[Row]), which may be no type at all, unless the project'sextend-genericsnames it; and a union that may be of values, with noNone, subscript, or builtin type in it, or aUnion[...]of one member. It leaves alone an alias with a quoted name as a member of aUnion[...]orOptional[...], since ruff then writesstr | "Text", which raises when the alias is read. It sees one module at a time: where another module reads a rewritten alias at run time, that reader takes its__value__. Thetypestatement is Python 3.12's, so it does not run in a project whosetarget-versionis older.doc-commentswrites the comment directly above a name as a Sphinx doc comment, which editors and doc tools attach to the name:# How often a run is resumed.aboveMAX_RESUMES: Final = 3becomes#: How often a run is resumed.. The names are a module's constants and type aliases, among its own statements, and the fields of a@dataclass, aNamedTuple, or an enum. It leaves alone a field of any other class, since a pydantic model's description is the docstring below its field and a#above one is a note kept out of the schema; a comment above a function, an import, or a lowercase module object; a block that holds a#:line already, a directive to a tool (# noqa,# type:,# fmt:), or a section's banner; and a comment a blank line separates from the name. It cannot tell what a name is from why the code is so, so a note of the second kind is written with a blank line below it, or beside the value. The colon can push a line past the line length, which the lint then reports.general-paramswidens a parameter annotatedlist,dict,set,frozenset, ortuple[X, ...]that its function only reads to the most general type its reads need:def total(costs: list[float])whose body only sumscostsbecomesdef total(costs: Iterable[float]). One pass isIterable, and a star is one pass:*pamong a call's arguments or in a list, tuple, or set display.len,in, a truth test, or more than one pass isCollection; indexing, slicing, orreversedof a list or a tuple isSequence; a dict only read,**pamong a call's arguments or in a dict display included, isMapping; and a set tested withinisAbstractSet, neverCollection. A parameter that may beNone,costs: list[float] | None, keeps its| Noneand is read the same way:costs is Noneneeds nothing of it, andcosts or otheris read ascostsis, sosum(costs or [])givesIterable[float] | None; one given its default by any other means, bound again or through a conditional expression, is left alone, as is any other union. It is deliberately narrow, since a wrong widening fails the type checker at a call site: it leaves alone a parameter used in any way it does not know to be a read (mutated, stored, returned, yielded, compared, unpacked by an assignment, or passed to any call but a few builtins whose names the module does not bind), so a tuple used as a tuple, hashed, used as a key, or concatenated, stays one, as does a tuple of a fixed length; it also leaves alone one bound again or named by a nested function, one never read, one whose function returns the type it takes, every parameter of a function with a decorator but a few plain ones (a click command, an override), every parameter of a function the module names other than to call it (typer.run(main),Thread(target=self.run)), since what takes the function may read its annotations as the program runs, and a method of a class with a base, or of one the module derives another class from, which may override or be overridden. The import goes underif TYPE_CHECKING:where the module has that block and postpones its annotations.empty-collectionsannotates an empty[],{}, orset()that a function fills, which a strict pyright reports as partly unknown and no tool annotates:found = []that a function annotated-> list[Path]returns becomesfound: list[Path] = []. The type comes from the return annotation, where the name is returned under its own type, an abstract one such asSequence[Path], or either| None, or returned throughsorted,list,tuple,set, orfrozenset; or from a string'sjoin, which makes the itemsstr. The rules that apply must agree. It leaves alone a name its function binds more than once, or uses in any way but a known few (filled by.appendand its kin as a statement, stored to by[k] =ordel, measured, tested, searched within, joined, or returned), a return annotation that is quoted, an alias, a wider union, or namesAny, an element type the function or its class binds, a generator, and a function with a checker's or a lint's directive on its header, on the assignment, or on thereturn. Python never evaluates a local's annotation, so nothing outside the function sees it.explicit-override, off by default, puts@overrideon a method that overrides one of a base class the module defines, so the type checker reports it once the base's is renamed; mypy, pyrefly, and basedpyright each report an override without it, and none writes it. It importsoverridefromtypingwhere the module has no import of it. A base is a class among the module's own statements, bound once, above the class that derives from it, with no decorator but one that hands the class back (dataclass,final,total_ordering,runtime_checkable); a method overrides when such a base, directly or through its own, defines it once. So most overrides, of a base imported from elsewhere, are not marked. It leaves alone a method with a decorator, one its class binds again,__init__and__new__, one with an unannotated parameter, whose type pyrefly would then take from the base's, and one with a checker's or a lint's directive on or above its header, and a module with a star import or another binding ofoverride.typing.overrideis Python 3.12's, so it does not run where thetarget-versionis older.
Each is opinionated, and each is conservative: where it cannot be sure a rewrite is right, it leaves the code alone
and reports nothing. The cases each one is held to are Python snippets, before and after, in
tests/cases/.
Your project's own codemods
A convention that only your project has, such as naming an enum's member where its code compares a bare string, is a
codemod of your own that brash runs with its own. Register it in the project's pyproject.toml, or in the
[codemods] of its brash.toml, under the name the command line takes, as the module that holds it and the
class's name there:
[tool.brash.codemods]
no-print = "codemods.no_print:NoPrint"
The module is imported as any other is: from the environment brash runs in, or from the directory that holds the
pyproject.toml, which brash puts last on the import path, so codemods/no_print.py beside it is found without
being installed. A project's
codemods run after brash's own, in the table's order, and --only and --list take them as they take brash's.
The class is a brash.command.Command, which is a
libcst.codemod.VisitorBasedCodemodCommand: its visit_
and leave_ methods rewrite the tree, and AddImportsVisitor.add_needed_import adds an import it needs.
import libcst as cst
from brash.command import Command
class NoPrint(Command):
"""Remove every `print(...)` statement."""
DESCRIPTION = "Remove `print` statements."
@classmethod
def wants(cls, source: str) -> bool:
return "print(" in source
def leave_Expr(self, original_node: cst.Expr, updated_node: cst.Expr) -> cst.Expr | cst.RemovalSentinel:
match updated_node.value:
case cst.Call(func=cst.Name(value="print")):
return cst.RemoveFromParent()
case _:
return updated_node
Two rules keep the check honest and quick, for your codemods as for brash's:
- A codemod leaves alone what it wrote.
--checkfails on any diff, so a second run over its own output must change nothing, also once the formatter has sorted the imports and formatted the result. - Say cheaply when a file is not yours. Parsing a module with LibCST and visiting it takes a few hundred times as
long as reading it, and the lint runs every codemod over every file.
Command.wants(source)answers from the text alone, before anything is parsed:final-constantsdecides from the stdlib'sast, so a file with no constant left to annotate is never parsed by LibCST. A codemod without awantsparses every file on every lint. A codemod held to some files alone says which inCommand.covers(filename), asked before the text is read.
A codemod that reads one of the project's settings has them as self.config, a brash.config.Config, and answers
in Command.wants_in(source, config) where wants would need one: type-aliases reads extend-generics so.
A codemod that writes syntax an older Python does not have names the first that has it, as MINIMUM_PYTHON = (3, 12)
on the class, and does not run in a project whose target-version is older. One for a convention a project
chooses to hold, which most do not, says so as OPT_IN = True: it is off by default, and runs only where select
or extend-select names it, or --only does.
To test one, run a module's text through it: brash.run.rewrite(source, ["no-print"], codemods={"no-print": NoPrint})
gives the text back rewritten, and takes the settings to run under as config=.
A rewrite to run once, such as a migration off an API, needs no entry: LibCST's own
python -m libcst.tool codemod runs it, and you
commit the result.
An example: canopy's vocab-members
canopy keeps its statuses, kinds, and phases as StrEnums in one module,
core/vocab.py. A member of a StrEnum is the string it holds, so run.status == "running" type-checks and works,
and no linter catches the bare string where the code is meant to name the member. canopy's own codemod rewrites it:
if run.status == "running": # before
if run.status == RunStatus.RUNNING: # after, with RunStatus imported
It is one module, codemods/vocab_members.py,
at the root of canopy's repository, and one entry in its pyproject.toml:
[tool.brash.codemods]
vocab-members = "codemods.vocab_members:VocabMembers"
Three things in it are worth copying:
- It reads the project, without importing it. The vocabularies are whatever
core/vocab.pyholds today, read with the stdlib'sastfrom a path relative to the codemod's own file. A new member needs no change to the codemod, and nothing of canopy is imported or run to lint it. - It says which files are its own.
covers(filename)is true undersrc/canopyalone: the tests assert on the string on purpose, and the vocabulary's own module defines it. - It rewrites only what it can tie down. A string becomes a member where the other side of the comparison is annotated with one vocabulary, or is a field of a typed row. A string several vocabularies hold, with nothing to say which is meant, is left alone, as is any string outside a comparison.
Its tests are before-and-after snippets in Markdown, the same shape as brash's own in tests/cases/.
Other things a project's codemod can do
A codemod of your own fits wherever a review comment keeps being written and the fix is always the same edit. It sees one module at a time, as text and syntax, and may read other files of the project to learn its names.
- Keep a migration finished. Once every caller has moved from
old_client.get(...)toclient.fetch(...), a codemod that rewrites the old call keeps a new one from landing: the lint fails with the fix as its diff. - Send calls through the project's own seam.
subprocess.run(...)becomesprocs.run(...),datetime.now()becomesclock.now(),print(...)becomeslog.info(...): wherever the project has one module that is allowed to touch something and wants every other module to go through it. - Name a constant where a string is repeated. Feature flags, config keys, route names, metric names: read the
names from the module or file that declares them, as
vocab-membersdoes, and rewrite the bare string that equals one, in the calls that take them. - Import from the public module. Rewrite
from app.billing._internal import Invoicetofrom app.billing import Invoicewhere the package re-exports the name, so a private module can move. - Hold tests to one way of doing a thing.
from unittest.mock import patchbecomesfrom unittest import mockwithmock.patchat each use; atime.sleepin a test becomes the project'swait_until.coversholds it totests/. - Spell a framework's idiom one way. A logging call with an f-string becomes one with
%sarguments; a@pytest.fixture()loses its empty parentheses; aDepends(get_db)parameter gets the project'sAnnotatedalias. - Add what every module must have. A
from __future__ import annotations, a license header, a module logger named for__name__.
What does not fit: a rewrite that needs to know a value's type across modules, which is a type checker's question, and one that changes what the code does in a way a person should decide each time.
Adding a codemod to brash
A codemod belongs in brash when it holds code to a convention any Python project could adopt, and reads nothing but the module it rewrites.
- Write a module in
src/brash/holding a subclass ofcommand.Command, with aDESCRIPTION. - Add it to
registry.CODEMODSunder the name the command line takes. - Test it in
tests/, with its cases as Python snippets intests/cases/<name>.md: under a###heading, twopythonblocks are a module's text before and after, and one is text it must leave alone.helpers.cases(name)reads them for the test module, which runs each throughrun.rewrite(source, [name]). ruff does not format that file, so a snippet stays as the codemod writes it. - Describe it in this README, under The codemods.
- Run
make fmtand commit what it rewrote, since brash runs on its own code and the lint fails from then on wherever it would still change a file.
Development
uv sync # install brash and its dev tools
make fmt # brash on its own code, then ruff and rumdl
make lint # every formatter's check, ruff, brash --check, pyrefly, rumdl, actionlint, zizmor
make test # pytest
make docs # the documentation, into docs/_build/html
AGENTS.md holds the conventions.
Read the Docs builds docs/ on every push: this README, and the API a project's
codemod is written against. A release is a v* tag:
the Publish workflow builds it, with the tag as its version, and publishes it to PyPI. The Tag workflow, run by hand,
creates the tag on the head of main or a branch it is given: the next major, minor or patch version, or an exact
tag.
License
MIT.
Metadata
Release files for brash 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| brash-0.3.0.tar.gz | 186.9 kB | With provenance | Details for brash-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| brash-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for brash-0.3.0-py3-none-any.whl |
Total release size: 244.8 kB
Release files / brash-0.3.0.tar.gz
| Download URL | brash-0.3.0.tar.gz |
|---|---|
| Size | 186.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
01d4510ab841644517b9a576b8920dd763c2d2f6c7f3755a9ace66e0eddf9766
|
|
BLAKE2b-256 checksum How to use checksums |
970a48352e9b62fb4df1db1d1b4943bd2fcc3849b8a54e0b85054a7b10ad6f05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / brash-0.3.0-py3-none-any.whl
| Download URL | brash-0.3.0-py3-none-any.whl |
|---|---|
| Size | 57.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4e4eb02659f4bfe977e28ae732e956581b36909aca09fb81282742e26c4a8e09
|
|
BLAKE2b-256 checksum How to use checksums |
8d3a3a603d7e15617a5f5bb98253b6622c80b898539ae093d1b490a3beb7a120
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for brash 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for brash-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.