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.
Create and test a project rule
Section titled “Create and test a project rule”Initialize the rule folders, scaffold a no-print rule, and check app.py.
$ xpd rules new project --yesCreated 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 --yesCreated 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 casesimport logging
print("loaded")logging.info("loaded")$ xpd check app.py --format textapp.py:3:1: warning [local/no-print] Use the logger, so the output has a level and a time.xpd: 1 issueAdd valid and invalid examples to rule-tests/no-print-test.yml, write snapshots with --update-all, and verify with xpd rules test.
id: no-printvalid: - logging.info("loaded")invalid: - print("loaded")$ xpd rules test --update-allno-print: passedno-print: updated2 cases, 0 failed$ xpd rules testno-print: passedno-print: passed2 cases, 0 failedScope rules to one library pack
Section titled “Scope rules to one library pack”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.
$ 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.' --yesCreated rule mylib/old-api created /tmp/project/rules/mylib/old-api.yml created /tmp/project/rules/mylib/old-api.md modified /tmp/project/xpd.ymlAssign 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: featurescategoryPrefixes: mylib.datasets: data mylib.datasets.metrics: evaluation mylib.datasets.normalize: featuresContribute a rule to xpd
Section titled “Contribute a rule to xpd”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.
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 |
Index a new library pack
Section titled “Index a new library pack”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.
uv venv /var/tmp/xpd-source/venvuv pip install --python /var/tmp/xpd-source/venv/bin/python --no-deps scikit-lego==0.9.10uv run --with griffe==2.4.1 cargo xtask index /var/tmp/xpd-source/venv/lib/python3.15/site-packages --package sklegocargo buildcargo xtask check-preprocessingtarget/debug/xpd find --write-vectors indexes/vectors.bincargo run --quiet -- find --check indexes/questions.json