willbooster-tree-sitter-python 24.0.0

Python grammar for tree-sitter
Documentation
# @willbooster/tree-sitter-python

[![npm version](https://img.shields.io/npm/v/@willbooster/tree-sitter-python.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-python)
[![license](https://img.shields.io/npm/l/@willbooster/tree-sitter-python.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-python)
[![Test](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test.yml)
[![Test rust](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test-rust.yml)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![wbfy](https://img.shields.io/badge/wbfy-22.1.1-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
[![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-python.svg)](https://crates.io/crates/willbooster-tree-sitter-python)

Python grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
[tree-sitter/tree-sitter-python](https://github.com/tree-sitter/tree-sitter-python). We are grateful
to its authors and contributors. This is not an official release of that project.

This fork builds on upstream v0.25.0 with grammar and scanner fixes, including Python 3.15
unpacking comprehensions. It ships a compact parser generated by the parser generator of
[WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter). Its ABI (`0x5742000e`) rejects upstream
Tree-sitter runtimes and WillBooster runtimes prior to 23.1.0. Use this parser with `willbooster-tree-sitter` or
`@willbooster/web-tree-sitter` 23.1.0 or later within 23.x, the runtime line it is tested and fuzzed with.

## Support Scope

This fork maintains only full parsing: valid input yields a correct tree, and invalid input yields `ERROR` or `MISSING`
nodes without crashing, hanging, or taking more than linear time. Incremental parsing and queries (highlights, locals,
injections, tags) are not supported. Node types, field names, and the trees that error recovery produces may change;
such changes are released as new major versions.

## Usage

The npm package ships `tree-sitter-python.wasm` for
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
browsers, and Cloudflare Workers. Package peer dependencies require `@willbooster/web-tree-sitter` `^23.1.0`.

In Node.js and Bun, load the grammar from its path:

```js
import { fileURLToPath } from 'node:url';

import { Language, Parser } from '@willbooster/web-tree-sitter';

await Parser.init();
const parser = new Parser();
const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-python/tree-sitter-python.wasm'));
parser.setLanguage(await Language.load(wasmPath));
const tree = parser.parse('def main():\n    pass\n');
```

In browsers, load the grammar from its URL. With Vite:

```js
import pythonUrl from '@willbooster/tree-sitter-python/tree-sitter-python.wasm?url';
import { Language, Parser } from '@willbooster/web-tree-sitter';
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';

await Parser.init({ locateFile: () => runtimeUrl });
const parser = new Parser();
parser.setLanguage(await Language.load(pythonUrl));
```

In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules:

```js
import python from '@willbooster/tree-sitter-python/tree-sitter-python.wasm';
import { Language, Parser } from '@willbooster/web-tree-sitter';
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';

await Parser.init({ wasmModule: runtime });
const parser = new Parser();
parser.setLanguage(await Language.load(python));
```

The package also ships the node types in `src/node-types.json`.

Qualified generic bases use `member_type` inside `generic_type`. Generic bases in annotations also recognize the contextual identifier spellings `match`, `print`, `exec` and `lazy`, while ordinary value subscripts retain their expression trees. Plain qualified generic arguments also expose `member_type` inside their `type` operand. Structured unpacked annotations and unpacked generic arguments use a `type` child inside `splat_type`. Expression-valued starred parameter annotations, such as `*args: *factory()`, keep a direct expression child instead. Ordinary value unpacks keep their expression tree.

In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-python) and on
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) 23.1.0 or later within 23.x:

```sh
cargo add willbooster-tree-sitter@23.1.0 --rename tree-sitter
cargo add willbooster-tree-sitter-python --rename tree-sitter-python
```

```rust
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_python::LANGUAGE.into())?;
```

## Development

```sh
mise install
bun install --frozen-lockfile
bun run build/ci
bun run test
script/parse-examples
cargo test
```

The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary that runs here. Run other CLI
commands through it as well (e.g. `script/tree-sitter parse file.py`).

`bun run generate` records a fresh generation profile from the applicable `test/corpus` cases and Git-tracked
Python files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`. After changing a grammar,
`tree-sitter.json`, a corpus case, or a tracked example,
regenerate and commit `src/`. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
and the release build regenerate the parsers before compiling them.

`bun run test` runs:

- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
- a check that the real-world Python files in `examples/`, the checked-in ones and those of the cloned repositories,
  fail to parse exactly as listed in `script/known-failures.txt`. The first run clones the repositories. The example
  repositories are pinned to commits in `script/parse-examples`. After a grammar change or a moved pin alters that list,
  `script/parse-examples` rewrites it; review its diff before committing;
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear time
  (ten times the lines take about ten times the CPU time, under a ceiling), since consumers parse files while they are
  being edited. It loads the Wasm build through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after
  regenerating the parser;
- a native CLI crash regression for deeply indented strings (`test/unit/deepIndentation.test.ts`);
- a check that `package.json` and `Cargo.lock` test the same runtime version (`test/unit/runtimeVersion.test.ts`);
- checks that the Wasm build parses in Chromium (`test/unit/browser/`) and in Cloudflare Workers with and without
  Node.js compatibility (`test/unit/workerd.test.ts`). Run `bun run test/ci-setup` once to install Chromium.

Tests and `script/parse-examples` keep native parser builds under this checkout's `.tmp/` instead of the CLI cache shared by every checkout.
See `test/unit/run.ts`, `test/unit/deepIndentation.test.ts`, and `script/parse-examples`
for their build locations and cleanup.

CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser and the scanner with the platform's
C compiler as the crate does for its users, and fuzzes the parser with libFuzzer and sanitizers
(`.github/workflows/robustness.yml`).

### References

- [Python 2 Grammar](https://docs.python.org/2/reference/grammar.html)
- [Python 3 Grammar](https://docs.python.org/3/reference/grammar.html)