Skip to content

Development

Set up

The project uses uv. From a checkout:

uv sync

This creates the environment with the development tools: pytest, Hypothesis, NumPy, ruff and mypy. NumPy is a development dependency only. The package itself depends on nothing.

Run the tests

uv run pytest

One command runs everything that is a test:

  • the unit and property tests in tests/;
  • the examples in the docstrings under src/;
  • the examples in README.md and in every page under docs/.

The run is parallel and takes a few seconds.

The property tests draw 400 examples per rule by default. A long run draws as many as you ask for:

SHAPERULES_EXAMPLES=20000 uv run pytest

Do a long run after changing a rule or the size algebra.

To run against another NumPy, as CI does:

uv run --with 'numpy==2.1.*' python -m pytest

Lint and type-check

uv run ruff check .
uv run mypy

mypy runs in strict mode on src/shaperules. CI runs both.

Regenerate the tables

src/shaperules/_tables.py holds the result types of NumPy's ufuncs and reductions. It is generated and must not be edited by hand:

uv run python tools/generate_tables.py

The script asks the installed NumPy and rewrites the file. Regenerate when a new NumPy adds a ufunc or changes a result type, and commit the new file. The test in tests/test_tables.py fails when the shipped tables differ from the NumPy under test, so a stale file cannot pass CI.

Generate with the newest NumPy the project supports. A ufunc that an older NumPy lacks is skipped when the tables are tested there.

Build the documentation

The site is built with MkDocs, Material for MkDocs and mkdocstrings. They are in the docs dependency group.

uv run --group docs mkdocs serve

serves the site locally and rebuilds on change.

uv run --group docs mkdocs build --strict

builds it into site/ the way CI does. --strict turns a broken link or a page missing from the navigation into a failure. The site is deployed to GitHub Pages by the Docs workflow on a push to main.

The API reference is generated from the docstrings, which are in Google style.

Write examples that are tests

Every Python example in the README and the documentation is a doctest and is run by uv run pytest. To add one:

  • Write it as a session inside a fenced python block: >>> lines, each followed by its exact output.
  • Leave a blank line between the last line of output and the closing fence. Without it the fence is read as part of the expected output.
  • Run the example and paste what it prints. Do not write an output from memory.
  • All the examples of one file share one namespace, from top to bottom. Import shaperules as sr once near the top of the page.
  • Show a symbolic shape with sr.fmt_shape or sr.fmt, or compare it with ==. The repr of a formula is exact and hard to read.

This page follows the convention:

>>> import shaperules as sr

>>> sr.fmt_shape(sr.matmul((2, "n"), ("n", "k")).value)
'(2, k)'

Long expected output can be wrapped at a space. Whitespace differences are ignored.

Releasing

A release is a version on PyPI. It starts with a pushed tag and ends with an approval, and both are the maintainer's.

  1. A pull request sets the version in pyproject.toml, the one place that holds it, and gives the version its section in CHANGELOG.md with the date. shaperules.__version__ reads the installed package's metadata.
  2. After the pull request is merged, the maintainer tags that commit v<version> and pushes the tag.
  3. The tag starts release.yml. It checks the tag against pyproject.toml, the commit against main and the changelog for the version's section (tools/release_verify.py), builds the wheel and the source distribution, installs the wheel alone on each supported Python with no NumPy present, and runs the test suite against the installed wheel.
  4. The pypi job then waits for the maintainer's approval and uploads. A GitHub release follows, with the version's changelog section as its text.

No PyPI token exists. PyPI accepts an upload for shaperules only from that workflow in the pypi environment of this repository (trusted publishing), the environment accepts only v* tags and needs the approval, and a ruleset lets only the maintainer create a v* tag.

PyPI never accepts the same version twice. If a run fails before the upload, delete the tag, fix the problem on main and tag again. After the upload a fix is a new version.

The workflow also runs on pull requests that touch the package, without the last two jobs, so a wheel that does not build or install is caught before a tag exists.

Repository layout

Path Holds
src/shaperules/forms.py The three canonical forms and their arithmetic.
src/shaperules/dims.py Sizes: integers, forms and ANY. Parsing, printing, arithmetic, comparisons.
src/shaperules/outcome.py The three answers and Refusal.
src/shaperules/_assemble.py Joining per-axis answers into the answer for a shape.
src/shaperules/elementwise.py broadcast, broadcast_pair, match_pair, matmul.
src/shaperules/axes.py Reductions, running totals, transposes, squeeze, expand_dims.
src/shaperules/reshaping.py reshape, concatenate, stack.
src/shaperules/indexing.py index, slice_length.
src/shaperules/dtypes.py Promotion, casting, and the lookups into the tables.
src/shaperules/_tables.py The generated tables.
tools/generate_tables.py The generator of the tables.
tests/oracle.py The comparison of a rule's answer with NumPy for every assignment.
tests/test_shape_rules.py One property test per shape rule.
tests/test_dims.py Property tests of the size algebra.
tests/test_dtypes.py The exhaustive comparisons of element types.
tests/test_tables.py The shipped tables against the NumPy under test.
tests/test_outcome.py The three answers, unwrap and best.
docs/, mkdocs.yml This site.

Add a shape rule

  1. Write the rule in the module it belongs to. It takes shapes as ShapeLike | str, reads them with as_shape, and returns an Outcome[Shape].
  2. Answer Known or Refused only for what holds for every assignment of the names, with 0, 1 and equal names in mind. Answer Unknown otherwise, with a reason, and with a likely reading where there is one.
  3. Give each new refusal a kind and the name of the exception NumPy raises.
  4. Add a property test to tests/test_shape_rules.py that passes the rule's answer and the real NumPy call to check. The oracle does the rest.
  5. Do a long run. Read every failure as a fact about NumPy first.
  6. Export the rule from shaperules/__init__.py. Document it in docs/guide/shape-rules.md, add its refusals to docs/reference/refusals.md, and add a line to CHANGELOG.md.

Writing style

The documentation is plain and concrete. Say what a thing does and where its limits are. Use short declarative sentences. Define a term the first time it appears. Use a table only for facts that can be listed. Do not claim anything the code does not do. When unsure, read the code or run it.