Skip to content

Write a rule

Run xpd rules new project and xpd rules new rule to turn any code pattern into a tested lint rule that runs in xpd check and xpd lsp. Every rule is an ast-grep YAML file paired with a Markdown explanation.

xpd rules new projectxpd rules new rule <id>Add valid and invalid casesxpd rules test --update-allxpd check reports local/<id>

Initialize the rule folders, scaffold a no-print rule, and check app.py.

Terminal window
$ xpd rules new project --yes
Created project
created /tmp/project/sgconfig.yml
created /tmp/project/xpd.yml
created /tmp/project/rules/.gitkeep
created /tmp/project/rule-tests/.gitkeep
created /tmp/project/utils/.gitkeep
$ xpd rules new rule no-print --language python --pattern 'print($X)' --message 'Use the logger, so the output has a level and a time.' --with-test --yes
Created rule no-print
created /tmp/project/rules/no-print.yml
created /tmp/project/rules/no-print.md
created /tmp/project/rule-tests/no-print-test.yml
.
├── sgconfig.yml ruleDirs: rules, utilDirs: utils, testConfigs: rule-tests
├── xpd.yml project packs
├── rules/no-print.yml ast-grep rule definition
├── rules/no-print.md explanation shown on editor hover and in xpd rules
└── rule-tests/no-print-test.yml valid and invalid test cases
app.py
import logging
print("loaded")
logging.info("loaded")
Terminal window
$ xpd check app.py --format text
app.py:3:1: warning [local/no-print] Use the logger, so the output has a level and a time.
xpd: 1 issue

Add valid and invalid examples to rule-tests/no-print-test.yml, write snapshots with --update-all, and verify with xpd rules test.

rule-tests/no-print-test.yml
id: no-print
valid:
- logging.info("loaded")
invalid:
- print("loaded")
Terminal window
$ xpd rules test --update-all
no-print: passed
no-print: updated
2 cases, 0 failed
$ xpd rules test
no-print: passed
no-print: passed
2 cases, 0 failed

Pass --pack <module> so a rule runs only on files that import that library, and add --fix and --fix-safety for automatic rewrites. See xpd rules new rule and xpd check.

Terminal window
$ xpd rules new rule old-api --pack mylib --pattern 'mylib.old($X)' --fix 'mylib.new($X)' --fix-safety safe --message 'old() was removed in 2.0.' --yes
Created rule mylib/old-api
created /tmp/project/rules/mylib/old-api.yml
created /tmp/project/rules/mylib/old-api.md
modified /tmp/project/xpd.yml

Assign categories on a pack with --category or in xpd.yml (pack.yml for built-in packs). The longest dotted module prefix wins, falling back to defaultCategory and then workflow.

An exact symbol path adds a category to that symbol. Indexed transformer classes can also appear in features based on their fit and transform methods. Assign a search wrapper to evaluation by its exact symbol path when it delegates transformation to a selected estimator. The wrapper stays out of feature discovery.

defaultCategory: features
categoryPrefixes:
mylib.datasets: data
mylib.datasets.metrics: evaluation
mylib.datasets.normalize: features

Built-in packs live under rules/<module>/ and compile into the binary at build time. Add a new pack folder to website/src/packs.mjs so the site lists it, mark expected sample lines with # xpd: <rule-id> under samples/<module>/, and verify with cargo build and python3 .github/scripts/check_sample_marks.py.

NoYesYesNoMessage names onerewritewith all inputs in matchedcode?Manual fix (no fix:)Fixed code produces anequal valueof the same type in allcases?Safe fix (metadata.fix: safe)Unsafe fix (metadata.fix:unsafe)

xpd re-parses every rewritten file with LibCST and drops any fix that produces invalid Python syntax.

Rule or pack setting Effect
fix: with metadata.fix Declares an ast-grep rewrite as safe or unsafe
aliases and members in pack.yml Limits fixes writing aliases like pd. or bare imports to files that bind them
metadata.since and metadata.until Gates the rule by the library version in the project's virtual environment
files and ignores Restricts matching paths with globs (**/ matches at any depth)
everyFile: true in pack.yml Runs the pack on every file without checking imports
alsoImports in pack.yml Also runs the pack on files that import one of the listed modules, as alsoImports: [xgboost, lightgbm]
packages in pack.yml Warns when the virtual environment has the library but lacks a companion package

Review the pinned wheel and tagged source before generating indexes/<package>.json. Add a row to the preprocessing inventory with the release evidence, expected public names, and reasons for exclusions. Use real task phrases for representative discovery checks. Include an exclusion reason when the library has no model-input, target, grouping, or training-weight APIs.

Index generation checks the candidate before writing under indexes/. After rebuilding, run cargo xtask check-preprocessing to check exact lookup, categories, and task discovery through the bundled CLI. Then regenerate vectors and run the full discovery check.

Terminal window
uv venv /var/tmp/xpd-source/venv
uv pip install --python /var/tmp/xpd-source/venv/bin/python --no-deps scikit-lego==0.9.10
uv run --with griffe==2.4.1 cargo xtask index /var/tmp/xpd-source/venv/lib/python3.15/site-packages --package sklego
cargo build
cargo xtask check-preprocessing
target/debug/xpd find --write-vectors indexes/vectors.bin
cargo run --quiet -- find --check indexes/questions.json