xpd check
Run xpd check after editing Python files to report slow or wrong library calls and optionally rewrite them. xpd skips .gitignore paths and activates each library pack only on files that import that library.
xpd check [OPTIONS] [PATHS]...Aliases: xpd scan.
Arguments
Section titled “Arguments”| Argument | What it does |
|---|---|
<PATHS>... |
Files or directories (directories respect .gitignore) |
Options
Section titled “Options”| Option | Default | What it does |
|---|---|---|
--format <FORMAT> |
Output format. Default: text on a terminal, json otherwise. Values: text, json, sarif, github. |
|
--fix |
Apply safe fixes, and report what is left. | |
-i, --review |
Review each proposed fix before writing it. | |
--unsafe-fixes |
Also count unsafe fixes, which may change behavior: apply them with --fix, or mark them as fixable without it. | |
--workspace |
Apply workspace Python codemods across local callers after single-file fixes. Requires --fix and --unsafe-fixes. Conflicts with --review. |
|
--rule <NAME> |
Run only this package or rule, as pandas, chained-assignment or pandas/chained-assignment. Repeat it to name several. Repeatable. |
|
--severity <SEVERITY> |
Run only the rules of this severity and above: error, warning, info or hint. | |
--fail-on <FAIL_ON> |
error |
Exit 1 when an issue of this severity or above is left: error, warning, info or hint. |
--category-summary |
Count remaining issues by category on stderr. An issue can count in several categories. | |
-c, --config <CONFIG> |
The project file to read instead of the nearest sgconfig.yml. | |
-j, --jobs <NUM> |
Maximum worker threads to use (overrides RAYON_NUM_THREADS and the default cap of 8) Aliases: --threads. |
Examples
Section titled “Examples”$ xpd check samples/bad.py --unsafe-fixes --format textsamples/bad.py:5:15: warning [pandas/row-loop] iterrows() builds a Series for every row. Compute on whole columns instead, such as df["a"] * df["w"]. When each row has to become a Python object, loop over itertuples() or to_dict("records").samples/bad.py:6:5: error [pandas/chained-assignment] [*] Chained assignment writes to a copy under copy-on-write, so the frame keeps its old values. Assign to df.loc[i, "a"] instead....xpd: 5 issues, 2 fixable with --fix --unsafe-fixes$ xpd check samples/bad.py --format json | jq -c '{fixed, fixable, issue: .issues[1]}'{"fixed":0,"fixable":0,"issue":{"column":5,"file":"samples/bad.py","fix":"unsafe","line":6,"message":"Chained assignment writes to a copy under copy-on-write, so the frame keeps its old values. Assign to df.loc[i, \"a\"] instead.","package":"pandas","rule":"chained-assignment","severity":"error"}}[*] marks issues fixable under the current flags. Use --format sarif for code scanning tools and --format github for pull request annotations.
Fix modes
Section titled “Fix modes”| Flags | Behavior |
|---|---|
| None | Report issues ([*] marks safe fixes) |
--fix |
Apply safe fixes, then report remaining issues |
--unsafe-fixes |
Report issues ([*] marks safe and unsafe fixes) |
--fix --unsafe-fixes |
Apply safe and unsafe fixes, then report remaining issues |
--fix --review |
Prompt interactively before writing each fix |
When a recorded module shrinks, --fix updates its statement ceiling in pyproject.toml. Once the module and all its functions fit their limits, the fix removes the entry.
$ mkdir -p ratchet && cd ratchet$ awk 'BEGIN { for (i = 1; i <= 301; i++) print "value_" i " = " i }' > pipeline.py$ printf '[tool.xpd.ratchet."pipeline.py"]\nstatements = 306\n' > pyproject.toml$ xpd check --fix pipeline.py --format json{"fixable":0,"fixed":1,"issues":[]}$ cat pyproject.toml[tool.xpd.ratchet."pipeline.py"]statements = 301$ cd ..Project rules
Section titled “Project rules”xpd loads local rules from the nearest sgconfig.yml or sgconfig.yaml (or --config). Scaffold a rule project with xpd rules new project.
| File or key | Contents |
|---|---|
ruleDirs in sgconfig.yml |
Plain ast-grep rules for any language |
utilDirs in sgconfig.yml |
Shared utility rules referenced via matches: |
xpd.yml beside sgconfig.yml |
Library-scoped packs with since, until, aliases, and members |
House style
Section titled “House style”House style rules report as info by default. Set style = "strict" to promote them to warning across xpd check and xpd lsp.
| Priority | File | Key |
|---|---|---|
| 1 | Nearest pyproject.toml with [tool.xpd] |
style = "strict" or "default" |
| 2 | $XDG_CONFIG_HOME/xpd/config.toml (%APPDATA%\xpd\config.toml on Windows) |
Top-level style = "strict" or "default" |
import pandas as pd
df = pd.read_csv("orders.csv")df = df[df["price"] > 0]df = df[df["qty"] >= 2]df = df[df["city"] != "Oslo"]$ xpd check script.py --format textscript.py:4:1: info [pandas/filter-ladder] df is filtered three or more times in a row, each time into itself, so every line leaves a half-filtered frame under the final name. Write one chain, as in df = df.loc[lambda d: ...].loc[lambda d: ...].loc[lambda d: ...].xpd: 1 issue$ printf '[tool.xpd]\nstyle = "strict"\n' > pyproject.toml$ xpd check script.py --format textscript.py:4:1: warning [pandas/filter-ladder] df is filtered three or more times in a row, each time into itself, so every line leaves a half-filtered frame under the final name. Write one chain, as in df = df.loc[lambda d: ...].loc[lambda d: ...].loc[lambda d: ...].xpd: 1 issueSuppress an issue
Section titled “Suppress an issue”Add # ast-grep-ignore: <pack>/<rule> on the line above (or inline after code) to silence one rule. Unused suppressions report as xpd/unused-suppression unless --rule or --severity filtered the run.
Exit status
Section titled “Exit status”| Exit code | Meaning |
|---|---|
0 |
No issue at or above --fail-on severity remains after fixes |
1 |
At least one issue at or above --fail-on severity remains (default: error) |
2 |
Invalid configuration or unreadable file (other readable files are still checked) |