This file provides guidance to agents when working with code in this repository.
formulate converts expressions between ROOT/TTreeFormula syntax, NumExpr syntax, and
(output-only) plain Python/NumPy. Public API is formulate.from_root(str) /
formulate.from_numexpr(str) returning an AST, then .to_root(), .to_numexpr(),
.to_python(), or the .variables / .named_constants / .unnamed_constants properties.
There is also a formulate CLI (src/formulate/cli.py).
pip install -e . --group dev # dev install
prek install # hook runner; reads .pre-commit-config.yaml
pytest # all tests
pytest tests/test_root.py -k tmath_min # single file / single test
pytest --cov=formulate # with coverage; fails under 100%
nox # default: lint + pylint + tests
nox -s tests # tests in an isolated env
nox -s docs -- --serve # build and serve the Sphinx docs
nox -s coverage
prek run --all-files # ruff, ruff-format, mypy (strict, src only), codespell, zizmorprek is a drop-in, much faster reimplementation of pre-commit — same
.pre-commit-config.yaml, same hook IDs, same subcommands. It is what the dev group installs
and what nox -s lint runs. pre-commit.ci (configured by the ci: block in the config) still
runs pre-commit proper on pull requests, so hooks must stay compatible with both.
tests/test_constants.py is the only file that evaluates expressions with the real engines.
It imports numexpr at module scope — so numexpr is in the test group, not just docs,
or the suite cannot be collected at all — and importorskips ROOT, which CI installs only
on Linux/Python 3.11. So --group test runs everything except the ROOT half, which skips.
Pipeline, in order:
- Grammar —
src/formulate/resources/{root,numexpr}_grammar.lark, parsed by lark's LALR parser (cached per backend in__init__.py). Each grammar encodes its own language's precedence: ROOT follows C++ (&&/||bind looser than comparisons,^is exponentiation,!is logical not), NumExpr follows Python (&/|bind tighter than comparisons,^is XOR, chained comparisons are rejected). Rule aliases (-> add,-> inv,-> multi_out) are the canonical operator names used downstream. toast.py— walks the lark parse tree and emits the backend-neutral AST. This is where surface names are normalized: namespaces (TMath::), the trailing$on ROOT array functions, function aliases (atan2→arctan2), and constant aliases (e_num→exp1). Unknown names raise here.toastitself is one_traversal.foldcall;_expandis what handles a single parse-tree node, returning the children still to convert plus a builder that assembles the AST node from them.AST.py— six frozen dataclass node types (Literal,Symbol,UnaryOperator,BinaryOperator,Matrix,Call) plus a_Backenddescriptor. A node type implements exactly three things:_children(),_format(*parts)forstr(), and_serializer(backend), which returns the builder that joins its already-serialized children. Every traversal lives on the base class and is iterative:__str__and_to_backendare both_traversal.foldcalls, and_walkbacksvariables/named_constants/unnamed_constants. Nothing here recurses, so depth is bounded by memory, not by the stack. Nodes are deliberately not comparable or hashable:__eq__raises and__hash__isNone, because structural equality would calla + bandb + adifferent expressions while semantic equality is computer algebra, well outside what a syntax translator can promise. Comparestr(node)or ato_*rendering instead — serialization is canonical, which is what the tests do. The nodes are declaredeq=Falseso the dataclass does not generate an__eq__/__hash__that would override that; a new node type must repeat it. The three publicto_*methods just pass the corresponding_Backendinstance (_ROOT,_NUMEXPR,_PYTHON). There is no per-backend visitor class — a new backend is a new_Backendliteral plus new tables. Every node validates in_serializer, which runs before its children are visited, so an expression with more than one unsupported construct reports the outermost one. Keep new checks there: the builder_serializerreturns should only join strings, and moving a check into it would silently change which error surfaces.identifiers.py— the hand-maintained lookup tables.FUNCTIONS/CONSTANTSare the canonical name sets;ROOT_*/NUMEXPR_*/PYTHON_*map canonical names to each backend's spelling. A name absent from a backend's table is how "unsupported" is expressed — the serializer raisesValueErroron theNonelookup.
Cutting across all of it, _traversal.py holds the single bottom-up walk both stages 2 and 3
are built on. It is underscore-prefixed because unlike the modules above it names no part of
the conversion — fold is a generic utility, is not documented, and nothing outside the
package should import it.
Two invariants worth knowing before editing:
- The AST is canonical, not any backend's dialect. Node operator/function strings
(
"add","inv","tmath_min") are internal identifiers. Never leak a backend spelling into the AST or intotoast.py. - Serialization is fully parenthesized and deterministic, which makes the serialized string
a canonical form. Most tests exploit this: they parse two spellings and assert the output
strings are identical, rather than evaluating numerically. Changing the parenthesization
rules (
_Backend.unparenthesized_ops) will move a lot of expected strings.
Add the canonical name to FUNCTIONS/CONSTANTS, then an entry in each backend map where it
is supported, plus any spelling in FUNCTION_ALIASES / CONSTANTS_ALIASES /
CONSTANTS_FUNCTION_ALIASES. tests/test_identifiers.py drives every table entry through the
parser and cross-checks the tables against each other, so a declared-but-unmapped name fails
loudly. If the name has both a scalar TMath:: form and an array $ form (as with
Min/Max), it needs the tmath_-prefixed qualified variant — see _get_function_name —
and an entry in FUNCTION_DISPLAY_NAMES, since that canonical name is one nobody writes and
would be meaningless in an error message.
Each table also carries an attribute docstring, which is what the API page renders; the
name additionally needs a row in the tables in docs/guide/expressions.rst, which nothing
checks automatically.
docs/ is Sphinx (RTD theme, nox -s docs, -- --serve to serve). Two things to know
before editing it:
- Examples execute at build time. Narrative pages use
jupyter-execute, notcode-block, wherever an output is shown, so a stale example fails the build instead of lying on the website. Keep it that way — if you add an example with output, run it. - Which page owns what.
guide/expressions.rstis the reference for every supported operator, function and constant, and how each is spelled per backend;guide/issues.rstis the reference for where the languages disagree. Behaviour changes belong in one of those two. API pages areautomodule/autodataover the docstrings, so the docstring is where API prose goes, not the.rst.
Anything with no faithful equivalent in the target raises ValueError rather than emitting
something subtly different. The single documented exception is %, which converts silently
even though ROOT truncates its operands to integers and NumExpr does float modulo. See
docs/guide/issues.rst; that file is the reference for the cross-language gotchas and should
be kept in sync with behaviour changes.
exceptions.py builds ParseError with heuristic suggestions (debug_root, debug_numexpr)
based on regex-scanning the source expression — e.g. suggesting && when a ROOT expression
contains a lone &. New syntax that people commonly get wrong belongs here.
- Coverage is enforced at 100% for both project and patch (
codecov.yml, threshold 0), and locally too:[tool.coverage]inpyproject.tomlturns on branch coverage and setsfail_under = 100, so any--covrun —nox -s coverage, or CI — fails on a gap rather than only being caught by codecov after a push. That also means--covon a filtered subset of tests will fail; leave it off when running one test. The suite reaches 100% without ROOT installed, so every job in the matrix can meet it. Genuinely unreachable code is marked# pragma: no cover; prefer deleting dead branches. filterwarnings = ["error"]andxfail_strictare on — a new warning fails the suite.- mypy runs
--strictoversrconly; the package shipspy.typed. - Supported Python is 3.11+, and the CI matrix includes Windows and free-threaded 3.14 and 3.15.
numexpr has no 3.15 or 3.15t wheels yet, so on those jobs
uvbuilds it from the sdist. - No recursive tree walks.
_traversal.foldandAST._walkare the only two, and both use an explicit stack, so peak frame depth is a small constant whatever the expression size. Watch for walks arriving implicitly rather than being written: a dataclass-generated__eq__/__hash__would recurse through child fields, which is one more reason the nodes seteq=False.tests/test_performance.pyruns at CPython's default recursion limit on purpose and nests 1,000 levels deep, so a recursive walk added back anywhere fails there. The sizes in that file are bounded by its 3s timing budget on the slowest job (Windows/3.11 under coverage), not by the stack. - Version comes from git tags via
hatch-vcs(_version.pyis generated; do not edit).