Development¶
Set up¶
The project uses uv. From a checkout:
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¶
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.mdand in every page underdocs/.
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:
Do a long run after changing a rule or the size algebra.
To run against another NumPy, as CI does:
Lint and type-check¶
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:
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.
serves the site locally and rebuilds on change.
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
pythonblock:>>>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 sronce near the top of the page. - Show a symbolic shape with
sr.fmt_shapeorsr.fmt, or compare it with==. Thereprof a formula is exact and hard to read.
This page follows the convention:
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.
- A pull request sets the version in
pyproject.toml, the one place that holds it, and gives the version its section inCHANGELOG.mdwith the date.shaperules.__version__reads the installed package's metadata. - After the pull request is merged, the maintainer tags that commit
v<version>and pushes the tag. - The tag starts release.yml.
It checks the tag against
pyproject.toml, the commit againstmainand 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. - The
pypijob 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¶
- Write the rule in the module it belongs to. It takes shapes as
ShapeLike | str, reads them withas_shape, and returns anOutcome[Shape]. - Answer
KnownorRefusedonly for what holds for every assignment of the names, with 0, 1 and equal names in mind. AnswerUnknownotherwise, with a reason, and with a likely reading where there is one. - Give each new refusal a
kindand the name of the exception NumPy raises. - Add a property test to
tests/test_shape_rules.pythat passes the rule's answer and the real NumPy call tocheck. The oracle does the rest. - Do a long run. Read every failure as a fact about NumPy first.
- Export the rule from
shaperules/__init__.py. Document it indocs/guide/shape-rules.md, add its refusals todocs/reference/refusals.md, and add a line toCHANGELOG.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.