README.md

xpd

xpd checks Python code for slow operations and wrong results. It checks code that uses pandas, NumPy, SciPy, scikit-learn, scikit-lego, MAPIE, Category Encoders, linearmodels, arch, pymoo, plotly or Apache Hamilton. xpd can rewrite many of the lines it reports, and a coding agent can run it after every edit.

What xpd reports

samples/bad.py holds slow loops and pandas assignments that write to a copy. The diff below shows what xpd check samples/bad.py --fix --unsafe-fixes changes. Each comment names the rule that reports the line.

diff
1 import pandas as pd
2
3 df = pd.DataFrame({"a": [1, 2], "k": [1, 2]})
4 other = df.copy()
5 for i, row in df.iterrows(): # warning pandas/row-loop
6- df["a"][i] = row["a"] * 2 # error pandas/chained-assignment
7+ df.loc[i, "a"] = row["a"] * 2
8-df.dropna(inplace=True) # info pandas/no-inplace
9+df = df.dropna()
10 m = pd.merge(df, other, on="k") # warning pandas/merge-without-validate
11 out = pd.DataFrame()
12 for f in ["x.csv"]:
13 out = pd.concat([out, pd.read_csv(f)]) # warning pandas/concat-in-loop

Each message says what goes wrong and what to write instead.

text
1samples/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.

An issue has one of three severities, and a fix is either safe or unsafe.

WordMeaning
errorThe code gives a wrong result or raises. Fix it.
warningThe code is slow or easy to get wrong. Fix it unless you can say why it is right here.
infoA shorter or newer form exists.
safe fixKeeps what the code does. --fix writes it.
unsafe fixMay change what the code does. --fix --unsafe-fixes writes it, so read the diff afterwards.

Rules

Each library has one pack of rules, written for the library's current version. A library's pack runs only on files that import the library. The Python pack runs on every file.

PackOne of its rulesWhat the rule reports
pandaschained-assignmentdf["a"][i] = v writes to a copy, so the frame keeps its old values
NumPyappend-flattensnp.append without axis flattens both arrays
SciPyremoved-keywordtol= on the sparse iterative solvers was removed and raises TypeError
scikit-learnfit-on-testA fit on the test rows makes the score on those rows useless
scikit-legothresholder-scoreThresholder.score ignores threshold, so a search scores every threshold the same
MAPIEcalibrate-on-training-rowsconformalize on the training rows gives intervals that covered 0.48 of new rows where 0.90 was asked
Category Encodersordinal-mapping-pairsAn OrdinalEncoder mapping given as a list of pairs codes every row as -1.0
linearmodelsclustered-without-clusterscov_type="clustered" without clusters gives the robust standard errors under another name
archunscaled-returnsReturns as decimals made the optimizer stop with code 4 and give a mean of 4.02 where 0.056 was right
pymooobjectives-stacked-by-rowout["F"] = np.array([f1, f2]) in a Problem gives each solution the wrong values, and a least f2 of 0.003 came back where the true least is 10
plotlymarker-size-diameterA marker's size is its diameter, so markers for 10 and 20 were drawn with areas of 78.5 and 314.2 square pixels
Hamiltonnode-mutates-inputA node writes to one of its parameters, and Hamilton hands the same object to other nodes
Pythondict-str-anydict[str, Any] in a signature hides the keys the function uses

xpd rules lists every rule, and the rules page shows each one with code before and after.

Install

sh
1uv tool install xpd

uvx xpd check . runs it once without an install.

Commands

text
1xpd
2├── check [paths] lint files and folders, and rewrite with --fix
3├── rules list every rule with its documentation
4├── graph <files> print the dataflow of step code or Hamilton modules
5├── profile <data file> print dtypes, nulls, unique counts and memory per column
6├── equiv old.py new.py run two scripts and compare the `result` each one leaves
7├── hook lint the file that a Claude Code edit touched
8├── mcp start the MCP server
9├── lsp start the language server for editors
10└── skill print the agent skill for this version

check, rules and graph read the files and start no Python. profile and equiv run the project's Python and need pandas there. xpd <command> -h lists the flags of a command. The commands page explains the output and the exit codes.

Use in an editor

xpd lsp starts a language server, so an editor shows the issues while you type, with a quick fix for each fixable one and the rule's documentation on hover. The language server page has the setup for Neovim, Helix, Zed and VS Code.

Use with a coding agent

An agent can use xpd as a plugin, or through a hook, an MCP server and skills that you set up one at a time.

WayWhat the agent getsSetup
PluginThe MCP server and the skills togetherLoad this repository in a client that supports Agent Plugins, such as Codex
HookThe issues of each file, right after the editxpd hook in the Claude Code settings
MCP serverTyped tools, the rules, the skills and promptsxpd mcp
SkillsWhen to run each command, and Python style that a linter cannot checknpx skills add bjoernaagaard/xpd

Plugin

This repository is a plugin in the Agent Plugins format, version 1.0.0. A client that supports the format loads the folder and gets the MCP server and the skills. The plugin starts xpd with uvx, so the machine needs uv. Version 1.0.0 has no hooks, so the hook below stays a setting of its own.

text
1xpd, loaded as a plugin
2├── plugin.json the name and version of the plugin
3├── skills/ xpd, python-best-practices and python-review
4├── mcp.json starts the MCP server of this version with uvx
5└── .agents/plugins/marketplace.json lists the plugin for Codex

Codex installs it with two commands.

sh
1codex plugin marketplace add bjoernaagaard/xpd
2codex plugin add xpd@xpd

Hook

After each edit to a .py file, Claude Code sends the edit to xpd hook. When xpd finds issues, Claude Code shows them to the agent in the same turn.

text
1Claude Code edits features.py
2└── the PostToolUse hook runs xpd hook
3 ├── no issues: exit code 0
4 └── issues: exit code 2
5 └── Claude Code shows the issues to the agent

Add the hook to .claude/settings.json in your project.

json
1{
2 "hooks": {
3 "PostToolUse": [
4 {
5 "matcher": "Edit|Write",
6 "hooks": [{ "type": "command", "command": "xpd hook" }]
7 }
8 ]
9 }
10}

MCP server

xpd mcp starts an MCP server, so an agent calls xpd through typed tools and needs no shell. The server comes with xpd, and the command below adds it to Claude Code.

sh
1claude mcp add xpd -- uvx xpd mcp
text
1xpd mcp
2├── tools
3│ ├── check, fix lint, and write fixes after the user confirms unsafe ones
4│ ├── graph, profile, equiv the commands of the same names
5│ ├── rules, rule list the rules, and read one with its examples
6│ ├── contract, gates judge a run from a data file to a saved model
7│ └── show_issues, show_dataflow, show_columns tables and a chart, in hosts that draw them
8├── resources
9│ ├── xpd://rules/{package}/{rule} one rule's documentation
10│ └── skill://<name>/SKILL.md each skill that ships with xpd
11└── prompts
12 └── lint_and_fix, review, refactor_data_logic, add_to_dataflow, build_pipeline

The server takes every rule and every result from the xpd binary, so it matches the installed version. The MCP page covers other clients, the settings and HTTP. The code mode page is a first draft of a run from a data file to a saved scikit-learn model in one call.

Skills

The repository includes Agent Skills in skills/. The Python skills work without xpd. They also run xpd when it is installed.

SkillWhat it does
xpdSays when to run each xpd command. xpd skill prints it for the installed version.
python-best-practicesHolds the rules an agent follows when it writes or changes Python.
python-reviewReviews a file, package or diff, and cites the rule for every finding.
sh
1npx skills add bjoernaagaard/xpd

Documentation

The full guide is at https://bjoernaagaard.github.io/xpd/. It also explains how to write a rule.

License

You may use xpd under the MIT license or the Apache-2.0 license.