Run before pushing — the ungated Lint workflow enforces these on every PR
push, and its Lint Gate job is a required status check:
ruff check diff_diff tests
black diff_diff tests
mypy diff_diff- Tool versions are pinned exactly in the
devextra ofpyproject.toml(black,ruff,mypy) and mirrored in.github/workflows/lint.yml(all three; the sync is enforced byTestLintWorkflowPinSync).mypy diff_diffis enforced at zero errors by the Lint workflow's Mypy job. A version bump is a deliberate PR updating both surfaces together. Refresh withpip install -e ".[dev]". The pinned tools require Python >= 3.10 (dev tooling only; the library itself still supports Python 3.9). [tool.ruff.lint] select = ["E", "F", "I", "W"]is deliberately explicit and is load-bearing, not a restatement of ruff's defaults. Ruff 0.16.0 expanded its default selection from 59 rules to 413 across 38 linter families; the explicitselectis what keeps a ruff upgrade from silently enabling hundreds of new rules. Do not drop it to inherit the defaults. Widening the rule set is a deliberate PR of its own.[tool.ruff.lint.per-file-ignores]entries are deliberate (trop logger-before-imports E402, honest_did math-notationlE741,__init__re-export F401, conftest import ordering E402). Do not "fix" those patterns or remove the ignores.- The 2026-07 repo-wide normalization commits are listed in
.git-blame-ignore-revs; rungit config blame.ignoreRevsFile .git-blame-ignore-revsonce sogit blameattributes lines to their real authors. - Branches created before the normalization commits: rebase onto main, then
run
ruff check --fix+blackon your touched files before pushing.
When implementing new functionality, always include accompanying documentation updates.
README.md is the GitHub/PyPI first-impression surface. Keep it lean (~190 lines). Most new content does NOT belong here.
Only edit README.md when:
- A new estimator is added (one line in the
## Estimatorsflat catalog) - A new top-level capability lands (one paragraph in
## Diagnostics & Sensitivityor## Survey Support) - Hero image, badges, or top-of-fold value-prop changes
- Documentation links rot
If you find yourself adding a usage example, a parameter table, or a multi-paragraph explanation to the README, you are in the wrong file - those belong on RTD or in diff_diff/guides/llms.txt.
-
diff_diff/guides/llms.txt(AI-agent source of truth) - Add:- One-line catalog entry in the
## Estimatorssection with paper citation + RTD link - One-line entry in
## Diagnostics and Sensitivity Analysisif applicable - This file is published on RTD via
docs/conf.pyhtml_extra_pathand bundled in the wheel viaget_llm_guide()- it is the canonical machine-readable contract
- One-line catalog entry in the
-
docs/api/*.rst(technical source of truth) - Add:- RST documentation with
autoclassdirectives - Method summaries
- References to academic papers
- RST documentation with
-
docs/tutorials/*.ipynb- Update relevant tutorial or create new one:- Working code examples
- Explanation of when/why to use the feature
- Follow the "Tutorial conventions" section below (CI-enforced by
tests/test_tutorial_conventions.py), including a drift test for a new notebook - New notebooks are registered in
docs/tutorials/index.rst(NOTdocs/index.rst): add a toctree entry with a short display label to the matching group (Business Applications / Fundamentals / Advanced Methods / Study Design) plus agrid-item-cardin that group's card grid
-
docs/references.rst(bibliography source of truth) - Add:- Full citation under the appropriate sub-section (matches the
### Subsectionheadings already in that file) - Use the RST format:
**Author (Year).** "Title." *Journal*, vol(num), pages. <https://doi.org/X>
- Full citation under the appropriate sub-section (matches the
-
README.md- Add ONLY:- One line in the
## Estimatorscatalog with the paper citation and RTD link
- One line in the
-
changelog.d/- Add a release-note fragmentchangelog.d/YYYYMMDD-<slug>.md(seechangelog.d/README.mdfor the format). NEVER editCHANGELOG.md's## [Unreleased]section directly - it is pointer-only and CI-enforced. -
CLAUDE.md- Update only if adding new critical rules or design patterns. -
ROADMAP.md- Update only if shipping moves an item from planned to current. -
docs/doc-deps.yaml- Add source-to-doc mappings for the new module.
The documentation site's information architecture is machine-enforced; a red
docs-tests check means one of these was violated:
- The root
docs/index.rsttoctree lists ONLY the 5 section landing pages (Getting Started / Practitioner Guide / Tutorials / User Guide / API Reference). New top-level pages join a section landing page, never the root - every root entry becomes a navbar item and past 5 the theme regrows an unusable "More" dropdown. - Every tutorial notebook has BOTH a short-labeled toctree entry (<= 40 chars)
and a
grid-item-card, in the SAME group ofdocs/tutorials/index.rst. - The homepage "Supported Estimators" table stays 1:1 with the
docs/api/index.rstEstimators autosummary - new estimators need both. - A class documented with
:no-index:on a module page must keep a canonical autosummary entry indocs/api/index.rst, else:class:cross-references to it render as dead text.
Release notes are per-PR fragment files under changelog.d/ so that parallel
PRs never conflict on CHANGELOG.md:
- Write your entry as
changelog.d/YYYYMMDD-<slug>.md- one file per PR,### <Category>blocks with the bullets exactly as they should appear in the changelog. Format spec:changelog.d/README.md. CHANGELOG.md's## [Unreleased]section stays pointer-only between releases; any direct edit fails the docs-tests lane.- At release,
/bump-versionruns.claude/scripts/changelog_compile.py compile, which merges the fragments into the new version section (oldest-first within each category) and deletes them. - Rebasing a branch created before this system: move your old
## [Unreleased]bullet into a new fragment file.
Rollback (if the system is ever removed): paste the current fragments' blocks
back under ## [Unreleased] (dropping the pointer comment), delete
changelog.d/, .claude/scripts/changelog_compile.py, and
tests/test_changelog_fragments.py, revert the docs-tests.yml /
rust-test.yml filter entries and step, and restore the pre-change
bump-version steps from git history.
- Update relevant docstrings
- Add/update tests
- Add a
changelog.d/YYYYMMDD-<slug>.mdfragment (never edit## [Unreleased]directly) - If methodology-related: Update
docs/methodology/REGISTRY.mdedge cases section - README is almost never the right place - skip it unless the bug was in a README claim
For methods based on academic papers, always include:
- Full citation in
docs/references.rstunder the appropriate### Subsectionheading (NOT in README) - Reference in RST API docs with paper details
- Citation in tutorial summary
- Optional: methodology reference in
docs/methodology/REGISTRY.mdfor non-trivial design choices
Example format (RST):
- **Sun, L., & Abraham, S. (2021).** "Estimating Dynamic Treatment Effects in Event Studies with Heterogeneous Treatment Effects." *Journal of Econometrics*, 225(2), 175-199. https://doi.org/10.1016/j.jeconom.2020.09.006
The notebooks in docs/tutorials/ are teaching material: a reader should meet
the same idea the same way wherever it comes up. These conventions apply to
every new notebook and to any notebook you edit. The mechanical ones are
CI-enforced by tests/test_tutorial_conventions.py; that guard carries a
shrinking allowlist of pre-existing violations (KNOWN_VIOLATIONS) - when you
repair a notebook, delete its entries; never add one.
-
Opening. An H1 that matches the notebook's card label in
docs/tutorials/index.rst; one paragraph stating the question the notebook answers; a short "What you'll learn" list; a prerequisites line linking the tutorials it builds on. -
Estimand and assumptions before the first fit. Name the target parameter, state the identifying assumptions in plain words, and say what would violate them.
-
Check against the truth on synthetic data. State the true effect as a named constant where the data is built, and pass every generator argument that determines the truth explicitly, even at its default value (
generate_staggered_datadefaults todynamic_effects=True, so the true average effect is nottreatment_effect). Compare the estimate and its interval to the truth in the output; if the interval misses, say why. Real data comes from aload_*loader where one exists. No data files are added underdocs/tutorials/. -
Inference. Cluster panel fits at the unit.
DifferenceInDifferencesdoes not auto-cluster, so passcluster=; the estimators that cluster at the unit by default need nothing extra. Tutorial 01 explains why; elsewhere one line is enough, and any other choice is justified in prose. Bootstraps usen_bootstrap=999and a fixed seed (fewer only for runtime, and say so). When prose discusses a p-value, name the inference that produced it and check it against the estimator's REGISTRY section, because the count-based p-values differ. WithBvalid draws of whichcare at least as extreme as the estimate: the permutation test reports(c + 1) / (B + 1)(steps of1/(B+1), smallest value1/(B+1)); the SyntheticDiD placebo reportsmax(c / B, 1/(B+1))(steps of1/B, same floor). A p-value computed analytically from a resampled standard error (SyntheticDiD bootstrap and jackknife, ChangesInChanges) is not count-based and has no floor. -
Pre-trend tests are not pass/fail. A non-rejection is not evidence that parallel trends holds. Say so, and link the sensitivity tutorial (
05_honest_did) and the pre-test power tutorial (07_pretrends_power). -
Control groups. Never-treated and not-yet-treated comparisons rest on different identifying assumptions. Say what each restricts, as Callaway and Sant'Anna (2021, Assumptions 4 and 5) do: the not-yet-treated version also restricts pre-treatment trends across cohorts (later cohorts' untreated trends must match too), in exchange for a larger comparison group; the never-treated version does not. They suggest never-treated when that group is sizeable and not-yet-treated when it is missing or too small. Do not rank the two as "weaker" and "stronger": the paper does not.
-
Event studies: one idiom.
es = results.aggregate("event_study") # if the results class has aggregate() es = results # otherwise (SunAbraham, a TWFE event_study=True fit) plot_event_study(es)
Read the table with
es.to_dataframe()whenesis anEventStudyResultscontainer, and withresults.to_dataframe(level="event_study")otherwise (SunAbraham, WooldridgeDiD). Do not read rawevent_study_effectsdicts, rename the unified columns, or hand-draw a single estimator's event study in matplotlib; build multi-estimator overlays from the unified frames. Event time 0 is the first treated period - an estimator with a different native index states the mapping. The idiom is pinned on every event-study producer bytests/test_visualization.py::TestEventStudyIdiomContract. -
Warnings. No unqualified
warnings.filterwarnings("ignore")orsimplefilter("ignore"). A warning that appears in committed output is either removed by fixing the call or explained in the next markdown cell. Two narrow exceptions: a scopedwarnings.catch_warnings()withcategory=andmessage=for documented platform noise, and a category-qualified filter with a comment for a third-party framework's deprecation noise. -
Closing. "Summary" (three to five bullets), "When to use it and when not to", "Next steps" linking other tutorials, and references. Business-track notebooks also keep their stakeholder-summary section.
- Code style. Import from top-level
diff_diff(a submodule import only for a symbol that is not exported there). Use full class names, not the short aliases. Import matplotlib plainly - noHAS_MATPLOTLIBguard. Print results withprint(results.summary())and read them asresults.att,.se,.conf_int,.p_value. - Reader voice. No version or migration narration, deprecation-ledger
ids, PR numbers, internal phase or wave names, internal paths, or
file:linereferences - the migration guide and the changelog own that history. Hyphens, not em dashes, in cell sources. Sentence-case, unnumbered section headings. - Committed outputs. Every notebook is committed executed: all code
cells run top to bottom in one pass (execution counts
1..n), no error outputs, no stray empty figures.nbsphinx_execute = "never", so the committed outputs are what Read the Docs shows. - Drift test. Every notebook has a
tests/test_t<NN>_<slug>_drift.pythat re-derives the numbers its prose quotes and asserts they appear in the rendered notebook (helpers intests/_tutorial_drift.py). Prose that would need an unpinned number should point at the output instead. - Numbering. The number in a filename is a stable id, not a reading
order (two notebooks are numbered 16). Reading order is the group order in
docs/tutorials/index.rst.
Three notebooks keep documented exceptions: 16_survey_did reads NHANES from
the tracked benchmark CSV (there is no loader for it), with a CDC download
only as the fallback; 29_mmm_calibration_pymc and
30_mmm_calibration_meridian have frozen code cells, because their committed
outputs come from long MCMC runs in framework-specific environments.
Committed outputs come from the Rust backend. Kernels are bound to a virtual environment, so register one for the checkout you are working in:
python -m ipykernel install --user --name my-diffdiff-kernelExecute into a scratch directory, compare with what is committed, then copy
the executed file back (never execute in place, and never pipe the run through
tail):
DIFF_DIFF_BACKEND=rust python - <<'PY'
from pathlib import Path
import nbformat
from nbclient import NotebookClient
out = Path("/tmp/executed")
out.mkdir(parents=True, exist_ok=True) # before the (possibly long) run
nb = nbformat.read("docs/tutorials/01_basic_did.ipynb", as_version=4)
NotebookClient(nb, timeout=3000, kernel_name="my-diffdiff-kernel",
resources={"metadata": {"path": "docs/tutorials"}}).execute()
nbformat.write(nb, out / "01_basic_did.ipynb")
PYRead every changed output against the prose around it, then update the
notebook's drift test (and, for 31-33, its code-cell hash pins). Before
opening the PR, mirror CI with
DIFF_DIFF_BACKEND=python pytest --nbmake docs/tutorials/<notebook>.ipynb.
- Don't just test that code runs without exception
- Assert the expected behavior actually occurred
- Bad:
result = func(bad_input)(only tests no crash) - Good:
result = func(bad_input); assert np.isnan(result.coef)(tests behavior)
- Test parameter appears in
get_params()output - Test
set_params()modifies the attribute - Test parameter actually affects behavior (not just stored)
- Capture warnings with
warnings.catch_warnings(record=True) - Assert warning message was emitted
- Assert the warned-about behavior occurred
Use assert_nan_inference() from conftest.py to validate ALL inference fields are
NaN-consistent. Don't check individual fields separately.
Storing a parameter is the easy half. Before implementing, grep for every place it will have to be honoured:
grep -rn "self\.<param>" diff_diff/<module>.pyA new parameter is only complete when it is:
- stored on
selfunder its own name —get_params()/set_params()then cover it automatically (the sharedBaseEstimatormixin introspects the__init__signature; a param stored under a DIFFERENT attribute name needs a_PARAM_ATTR_ALIASESentry, and constructor-derived state a_DERIVED_CONFIG_ATTRSentry — seediff_diff/_base.pyand the inheritance map inCLAUDE.md) - applied in every parameter-applying aggregation mode —
simple,event_study, andgroup.totalaccepts no independent aggregation parameters, but every parameter that changes the overall keeper set or aggregation mass (e.g.anticipation) must also be reflected — and tested — intotal's mass - applied in the bootstrap/inference paths, not just the analytical one
- reflected on the result object, so
to_dict()/summary()do not misreport it - propagated to the estimators that inherit it:
TwoWayFixedEffectsdefines no__init__of its own, so it inherits a newDifferenceInDifferencesconstructor parameter automatically (theMultiPeriodDiD3.9 shim that mirrored the signature was removed at the 4.0 cut);SyntheticDiDdefines its OWN signature (it forwards onlyalphatosuper().__init__, pinningcluster=None), so a new parent parameter must be explicitly mirrored — or explicitly rejected, like itsvcov_type/conley_*guards — there, andBaseEstimatorintrospects the concrete signature, so an unmirrored parent param simply won't appear inSyntheticDiD.get_params()
The recurring bug is a parameter that works in the default aggregation and is silently ignored in one of the others. The same trace applies when adding a new mode or code path: follow it through aggregation, bootstrap, and results before declaring it done.
When you add a new mode or code path (e.g. base_period="varying", a new
control-selection rule), verify the control/comparison group is composed correctly
for that path, not just the default. In staggered designs especially, confirm the
"not-yet-treated" comparison group excludes the treatment cohort itself, and that
parameter interactions (new mode × each aggregation method) select the intended
group. A silently mis-composed comparison group produces a plausible but wrong effect
with no error.
Wrap all related operations in np.errstate(), not just the one that raised.
A guard around the final division still lets an upstream matrix multiply or
subtraction emit the warning. Include division, matrix multiplication, and any
operation that can overflow or underflow.
Grep for every occurrence before fixing any of them, and fix them all in the same PR. Incremental fixes across review rounds are how a pattern bug survives: each round looks resolved, and the next reviewer finds the sibling you missed. If the same class of finding comes back a third time, stop patching sites and name the invariant instead.