shaperules 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shaperules-0.1.0/.github/workflows/ci.yml +77 -0
- shaperules-0.1.0/.github/workflows/docs.yml +49 -0
- shaperules-0.1.0/.github/workflows/release.yml +165 -0
- shaperules-0.1.0/.gitignore +8 -0
- shaperules-0.1.0/CHANGELOG.md +49 -0
- shaperules-0.1.0/LICENSE +21 -0
- shaperules-0.1.0/PKG-INFO +153 -0
- shaperules-0.1.0/README.md +128 -0
- shaperules-0.1.0/docs/contributing.md +196 -0
- shaperules-0.1.0/docs/get-started.md +179 -0
- shaperules-0.1.0/docs/guide/answers.md +253 -0
- shaperules-0.1.0/docs/guide/clients.md +334 -0
- shaperules-0.1.0/docs/guide/dtypes.md +383 -0
- shaperules-0.1.0/docs/guide/shape-rules.md +641 -0
- shaperules-0.1.0/docs/guide/sizes.md +387 -0
- shaperules-0.1.0/docs/index.md +132 -0
- shaperules-0.1.0/docs/internals/checked-against-numpy.md +298 -0
- shaperules-0.1.0/docs/internals/design.md +325 -0
- shaperules-0.1.0/docs/internals/limitations.md +239 -0
- shaperules-0.1.0/docs/reference/api.md +89 -0
- shaperules-0.1.0/docs/reference/refusals.md +153 -0
- shaperules-0.1.0/mkdocs.yml +113 -0
- shaperules-0.1.0/pyproject.toml +71 -0
- shaperules-0.1.0/src/shaperules/__init__.py +138 -0
- shaperules-0.1.0/src/shaperules/_assemble.py +163 -0
- shaperules-0.1.0/src/shaperules/_tables.py +933 -0
- shaperules-0.1.0/src/shaperules/axes.py +316 -0
- shaperules-0.1.0/src/shaperules/dims.py +716 -0
- shaperules-0.1.0/src/shaperules/dtypes.py +450 -0
- shaperules-0.1.0/src/shaperules/elementwise.py +127 -0
- shaperules-0.1.0/src/shaperules/forms.py +538 -0
- shaperules-0.1.0/src/shaperules/indexing.py +229 -0
- shaperules-0.1.0/src/shaperules/outcome.py +177 -0
- shaperules-0.1.0/src/shaperules/py.typed +0 -0
- shaperules-0.1.0/src/shaperules/reshaping.py +203 -0
- shaperules-0.1.0/tests/__init__.py +0 -0
- shaperules-0.1.0/tests/oracle.py +197 -0
- shaperules-0.1.0/tests/test_dims.py +205 -0
- shaperules-0.1.0/tests/test_dtypes.py +132 -0
- shaperules-0.1.0/tests/test_outcome.py +121 -0
- shaperules-0.1.0/tests/test_release.py +71 -0
- shaperules-0.1.0/tests/test_shape_rules.py +179 -0
- shaperules-0.1.0/tests/test_tables.py +40 -0
- shaperules-0.1.0/tools/generate_tables.py +135 -0
- shaperules-0.1.0/tools/release_verify.py +108 -0
- shaperules-0.1.0/uv.lock +1415 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
lint:
|
|
10
|
+
name: lint (ruff, mypy --strict)
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: astral-sh/setup-uv@v5
|
|
15
|
+
with:
|
|
16
|
+
enable-cache: true
|
|
17
|
+
- name: Run ruff
|
|
18
|
+
run: uv run ruff check .
|
|
19
|
+
- name: Type-check
|
|
20
|
+
run: uv run mypy
|
|
21
|
+
|
|
22
|
+
test:
|
|
23
|
+
# Every rule is held against the NumPy installed, so the suite runs on
|
|
24
|
+
# the oldest NumPy modelled and on the latest. The tables in
|
|
25
|
+
# src/shaperules/_tables.py must agree with both. The oldest is 2.1:
|
|
26
|
+
# NumPy 2.0 returns floats for floor, ceil and trunc of integers.
|
|
27
|
+
strategy:
|
|
28
|
+
fail-fast: false
|
|
29
|
+
matrix:
|
|
30
|
+
include:
|
|
31
|
+
- { python: "3.11", numpy: "2.1.*" }
|
|
32
|
+
- { python: "3.12", numpy: "2.1.*" }
|
|
33
|
+
- { python: "3.13", numpy: "2.1.*" }
|
|
34
|
+
- { python: "3.11", numpy: "latest" }
|
|
35
|
+
- { python: "3.13", numpy: "latest" }
|
|
36
|
+
- { python: "3.14", numpy: "latest" }
|
|
37
|
+
name: test (python ${{ matrix.python }}, numpy ${{ matrix.numpy }})
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
steps:
|
|
40
|
+
- uses: actions/checkout@v4
|
|
41
|
+
- uses: astral-sh/setup-uv@v5
|
|
42
|
+
with:
|
|
43
|
+
enable-cache: true
|
|
44
|
+
python-version: ${{ matrix.python }}
|
|
45
|
+
- name: Run the tests against NumPy
|
|
46
|
+
run: |
|
|
47
|
+
if [ "${{ matrix.numpy }}" = "latest" ]; then
|
|
48
|
+
uv run --python ${{ matrix.python }} pytest
|
|
49
|
+
else
|
|
50
|
+
uv run --python ${{ matrix.python }} --with 'numpy==${{ matrix.numpy }}' pytest
|
|
51
|
+
fi
|
|
52
|
+
|
|
53
|
+
wheel:
|
|
54
|
+
# The wheel is pure Python with no dependencies: it must install and
|
|
55
|
+
# answer in an environment that has no NumPy at all.
|
|
56
|
+
name: wheel (no NumPy installed)
|
|
57
|
+
runs-on: ubuntu-latest
|
|
58
|
+
steps:
|
|
59
|
+
- uses: actions/checkout@v4
|
|
60
|
+
- uses: astral-sh/setup-uv@v5
|
|
61
|
+
- name: Build
|
|
62
|
+
run: uv build
|
|
63
|
+
- name: Install the wheel alone and ask it a question
|
|
64
|
+
run: |
|
|
65
|
+
uv venv --python 3.11 /tmp/alone
|
|
66
|
+
uv pip install --python /tmp/alone/bin/python dist/*.whl
|
|
67
|
+
/tmp/alone/bin/python -c "
|
|
68
|
+
import sys, shaperules as sr
|
|
69
|
+
assert 'numpy' not in sys.modules
|
|
70
|
+
assert sr.matmul((2, 'n'), ('n', 5)).unwrap() == (2, 5)
|
|
71
|
+
assert sr.result_type('int8', 'uint8').unwrap() == 'int16'
|
|
72
|
+
print('ok', sr.__version__)
|
|
73
|
+
"
|
|
74
|
+
- uses: actions/upload-artifact@v4
|
|
75
|
+
with:
|
|
76
|
+
name: dist
|
|
77
|
+
path: dist
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: Docs
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
paths: ["docs/**", "src/**", "mkdocs.yml", "pyproject.toml", "uv.lock", ".github/workflows/docs.yml"]
|
|
7
|
+
pull_request:
|
|
8
|
+
paths: ["docs/**", "src/**", "mkdocs.yml", "pyproject.toml", "uv.lock", ".github/workflows/docs.yml"]
|
|
9
|
+
workflow_dispatch:
|
|
10
|
+
|
|
11
|
+
permissions:
|
|
12
|
+
contents: read
|
|
13
|
+
|
|
14
|
+
concurrency:
|
|
15
|
+
group: pages
|
|
16
|
+
cancel-in-progress: false
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
build:
|
|
20
|
+
# --strict turns a broken internal link or a page missing from the
|
|
21
|
+
# nav into a failed build, on pull requests too.
|
|
22
|
+
name: build (mkdocs --strict)
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v4
|
|
26
|
+
- uses: astral-sh/setup-uv@v5
|
|
27
|
+
with:
|
|
28
|
+
enable-cache: true
|
|
29
|
+
- name: Build the site
|
|
30
|
+
run: uv run --group docs mkdocs build --strict
|
|
31
|
+
- uses: actions/upload-pages-artifact@v3
|
|
32
|
+
if: github.event_name != 'pull_request'
|
|
33
|
+
with:
|
|
34
|
+
path: site
|
|
35
|
+
|
|
36
|
+
deploy:
|
|
37
|
+
name: deploy to GitHub Pages
|
|
38
|
+
if: github.event_name != 'pull_request'
|
|
39
|
+
needs: build
|
|
40
|
+
runs-on: ubuntu-latest
|
|
41
|
+
permissions:
|
|
42
|
+
pages: write
|
|
43
|
+
id-token: write
|
|
44
|
+
environment:
|
|
45
|
+
name: github-pages
|
|
46
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
47
|
+
steps:
|
|
48
|
+
- id: deployment
|
|
49
|
+
uses: actions/deploy-pages@v4
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
# Builds the shaperules wheel and source distribution and tests the wheel
|
|
4
|
+
# alone. For a pushed v* tag it then publishes them to PyPI, once the
|
|
5
|
+
# maintainer approves, and creates the GitHub release. See "Releasing" in
|
|
6
|
+
# docs/contributing.md.
|
|
7
|
+
#
|
|
8
|
+
# Who can publish. PyPI accepts uploads for shaperules only from the `pypi`
|
|
9
|
+
# job below: trusted publishing, configured on pypi.org for this repository,
|
|
10
|
+
# this file and the `pypi` environment. That environment needs the
|
|
11
|
+
# maintainer's approval for every run and accepts only v* tags, and only the
|
|
12
|
+
# maintainer may push a v* tag (a repository ruleset). No PyPI token exists
|
|
13
|
+
# anywhere in the repository or its settings.
|
|
14
|
+
#
|
|
15
|
+
# Third-party actions are pinned to a commit, so a moved tag upstream cannot
|
|
16
|
+
# change what runs with publishing rights.
|
|
17
|
+
|
|
18
|
+
on:
|
|
19
|
+
push:
|
|
20
|
+
tags: ["v*"]
|
|
21
|
+
# A change that could break the package builds and tests it before it
|
|
22
|
+
# merges. The README is its page on PyPI.
|
|
23
|
+
pull_request:
|
|
24
|
+
paths:
|
|
25
|
+
- "src/**"
|
|
26
|
+
- "pyproject.toml"
|
|
27
|
+
- "README.md"
|
|
28
|
+
- "CHANGELOG.md"
|
|
29
|
+
- "tools/release_verify.py"
|
|
30
|
+
- ".github/workflows/release.yml"
|
|
31
|
+
|
|
32
|
+
permissions: {}
|
|
33
|
+
|
|
34
|
+
concurrency:
|
|
35
|
+
group: release-${{ github.ref }}
|
|
36
|
+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
37
|
+
|
|
38
|
+
jobs:
|
|
39
|
+
version:
|
|
40
|
+
runs-on: ubuntu-latest
|
|
41
|
+
outputs:
|
|
42
|
+
version: ${{ steps.verify.outputs.version }}
|
|
43
|
+
prerelease: ${{ steps.verify.outputs.prerelease }}
|
|
44
|
+
steps:
|
|
45
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
46
|
+
with:
|
|
47
|
+
fetch-depth: 0 # the check below asks whether the tagged commit is on origin/main
|
|
48
|
+
persist-credentials: false
|
|
49
|
+
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
|
|
50
|
+
# For a tag: the tag names the version in pyproject.toml, the commit is
|
|
51
|
+
# on main, and the changelog has the version's section. Otherwise this
|
|
52
|
+
# only reads the version.
|
|
53
|
+
- id: verify
|
|
54
|
+
run: |
|
|
55
|
+
if [ "$GITHUB_REF_TYPE" = tag ]; then
|
|
56
|
+
uv run --no-project --python 3.13 python tools/release_verify.py --tag "$GITHUB_REF_NAME"
|
|
57
|
+
else
|
|
58
|
+
uv run --no-project --python 3.13 python tools/release_verify.py
|
|
59
|
+
fi
|
|
60
|
+
|
|
61
|
+
build:
|
|
62
|
+
needs: version
|
|
63
|
+
runs-on: ubuntu-latest
|
|
64
|
+
steps:
|
|
65
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
66
|
+
with:
|
|
67
|
+
persist-credentials: false
|
|
68
|
+
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
|
|
69
|
+
- name: Build the wheel and the source distribution
|
|
70
|
+
run: uv build
|
|
71
|
+
- name: Check the metadata PyPI will show
|
|
72
|
+
run: uvx twine check --strict dist/*
|
|
73
|
+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
|
74
|
+
with:
|
|
75
|
+
name: dist
|
|
76
|
+
path: dist/
|
|
77
|
+
if-no-files-found: error
|
|
78
|
+
|
|
79
|
+
test:
|
|
80
|
+
# The files that will be uploaded, installed outside the checkout. First
|
|
81
|
+
# the wheel alone, with no NumPy, which is the claim "no dependencies".
|
|
82
|
+
# Then the test suite against the installed wheel, with NumPy as the oracle.
|
|
83
|
+
needs: build
|
|
84
|
+
runs-on: ubuntu-latest
|
|
85
|
+
strategy:
|
|
86
|
+
fail-fast: false
|
|
87
|
+
matrix:
|
|
88
|
+
python: ["3.11", "3.12", "3.13", "3.14"]
|
|
89
|
+
steps:
|
|
90
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
91
|
+
with:
|
|
92
|
+
persist-credentials: false
|
|
93
|
+
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
|
|
94
|
+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
95
|
+
with:
|
|
96
|
+
name: dist
|
|
97
|
+
path: dist
|
|
98
|
+
- name: The wheel alone answers, with no NumPy installed
|
|
99
|
+
run: |
|
|
100
|
+
uv venv --python ${{ matrix.python }} /tmp/alone
|
|
101
|
+
uv pip install --python /tmp/alone/bin/python dist/*.whl
|
|
102
|
+
cd /tmp && /tmp/alone/bin/python -c "
|
|
103
|
+
import importlib.util, shaperules as sr
|
|
104
|
+
assert importlib.util.find_spec('numpy') is None, 'NumPy is installed'
|
|
105
|
+
assert '/tmp/alone/' in sr.__file__, sr.__file__
|
|
106
|
+
assert sr.matmul((2, 'n'), ('n', 5)).unwrap() == (2, 5)
|
|
107
|
+
assert sr.result_type('int8', 'uint8').unwrap() == 'int16'
|
|
108
|
+
print('ok', sr.__version__)
|
|
109
|
+
"
|
|
110
|
+
- name: The source distribution installs too
|
|
111
|
+
run: |
|
|
112
|
+
uv venv --python ${{ matrix.python }} /tmp/from-source
|
|
113
|
+
uv pip install --python /tmp/from-source/bin/python dist/*.tar.gz
|
|
114
|
+
cd /tmp && /tmp/from-source/bin/python -c "import shaperules as sr; print('ok', sr.__version__)"
|
|
115
|
+
- name: The test suite passes against the installed wheel
|
|
116
|
+
run: |
|
|
117
|
+
uv venv --python ${{ matrix.python }} /tmp/tested
|
|
118
|
+
uv pip install --python /tmp/tested/bin/python dist/*.whl numpy pytest hypothesis
|
|
119
|
+
/tmp/tested/bin/python -c "import shaperules; assert '/tmp/tested/' in shaperules.__file__, shaperules.__file__"
|
|
120
|
+
/tmp/tested/bin/python -m pytest -o addopts="" tests
|
|
121
|
+
|
|
122
|
+
pypi:
|
|
123
|
+
if: github.ref_type == 'tag'
|
|
124
|
+
needs: [version, test]
|
|
125
|
+
runs-on: ubuntu-latest
|
|
126
|
+
# The gate: this job waits until the maintainer approves it, and PyPI
|
|
127
|
+
# accepts an upload only from a job in this environment.
|
|
128
|
+
environment:
|
|
129
|
+
name: pypi
|
|
130
|
+
url: https://pypi.org/project/shaperules/${{ needs.version.outputs.version }}/
|
|
131
|
+
permissions:
|
|
132
|
+
id-token: write # trusted publishing: proves to PyPI which workflow and environment this is
|
|
133
|
+
steps:
|
|
134
|
+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
135
|
+
with:
|
|
136
|
+
name: dist
|
|
137
|
+
path: dist
|
|
138
|
+
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
|
|
139
|
+
with:
|
|
140
|
+
packages-dir: dist/
|
|
141
|
+
|
|
142
|
+
github-release:
|
|
143
|
+
needs: [version, pypi]
|
|
144
|
+
runs-on: ubuntu-latest
|
|
145
|
+
permissions:
|
|
146
|
+
contents: write # create the release
|
|
147
|
+
steps:
|
|
148
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
149
|
+
with:
|
|
150
|
+
persist-credentials: false
|
|
151
|
+
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
|
|
152
|
+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
153
|
+
with:
|
|
154
|
+
name: dist
|
|
155
|
+
path: dist
|
|
156
|
+
- env:
|
|
157
|
+
GH_TOKEN: ${{ github.token }}
|
|
158
|
+
VERSION: ${{ needs.version.outputs.version }}
|
|
159
|
+
PRERELEASE: ${{ needs.version.outputs.prerelease }}
|
|
160
|
+
run: |
|
|
161
|
+
uv run --no-project --python 3.13 python tools/release_verify.py --notes /tmp/notes.md
|
|
162
|
+
flags=()
|
|
163
|
+
if [ "$PRERELEASE" = true ]; then flags+=(--prerelease); fi
|
|
164
|
+
gh release create "$GITHUB_REF_NAME" dist/* --verify-tag \
|
|
165
|
+
--title "shaperules $VERSION" --notes-file /tmp/notes.md "${flags[@]}"
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to shaperules are recorded in this file. The format
|
|
4
|
+
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
5
|
+
|
|
6
|
+
## 0.1.0 (2026-12-01)
|
|
7
|
+
|
|
8
|
+
The first version. The rules were extracted from
|
|
9
|
+
[numty](https://github.com/sofeikov/numty).
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Sizes: plain integers, formulas over names, and `ANY` for a size nothing is
|
|
14
|
+
known about. Formulas are kept in one of three canonical forms, `LinForm`,
|
|
15
|
+
`DivForm` and `MonoForm`.
|
|
16
|
+
- `dim`, `shape`, `as_shape`, `sym`, `fresh`, `parse`, `fmt`, `fmt_shape`,
|
|
17
|
+
`is_known`, `substitute` and `substitute_shape`, and the arithmetic and
|
|
18
|
+
comparisons of `shaperules.dims`.
|
|
19
|
+
- The three answers `Known`, `Refused` and `Unknown`, with `unwrap` and
|
|
20
|
+
`best`. A `Refusal` has a stable `kind`, a `message` and the name of the
|
|
21
|
+
exception NumPy raises. `unwrap` raises `ShapeRefused` or `ShapeUnknown`.
|
|
22
|
+
- Shape rules: `broadcast`, `broadcast_pair`, `match_pair`, `matmul`,
|
|
23
|
+
`reduce` (with `needs_elements=True` for `max`, `min` and `ptp`, and
|
|
24
|
+
`scalar_axis=False` for `mean`, `std`, `var`, `median` and `average`),
|
|
25
|
+
`arg_reduce`, `cumulative`, `transpose`, `swapaxes`, `moveaxis`, `squeeze`,
|
|
26
|
+
`expand_dims`, `reshape` (with `-1`), `concatenate`, `stack`, `index` for
|
|
27
|
+
basic indexing, and `slice_length`.
|
|
28
|
+
- The axis helpers `normalize_axis` and `normalize_axes`.
|
|
29
|
+
- Element types: `result_type`, with Python `int`, `float` and `complex` as
|
|
30
|
+
NEP 50 weak scalars, `can_cast`, `ufunc_dtype` and `ufunc_dtypes` for every
|
|
31
|
+
element-wise NumPy ufunc of one or two inputs, and `reduce_dtype` for
|
|
32
|
+
sixteen reductions. Fourteen element types are covered, `bool` to
|
|
33
|
+
`complex128`.
|
|
34
|
+
- Tables of ufunc and reduction result types, generated from NumPy by
|
|
35
|
+
`tools/generate_tables.py`. They describe NumPy 2.1 and later.
|
|
36
|
+
- Tests that hold every shape rule against real NumPy calls for every
|
|
37
|
+
assignment of small sizes to the names, and compare every element-type
|
|
38
|
+
answer with NumPy.
|
|
39
|
+
- Documentation at <https://sofeikov.github.io/shaperules/>, with every
|
|
40
|
+
example run by the test suite.
|
|
41
|
+
|
|
42
|
+
### Not in this version
|
|
43
|
+
|
|
44
|
+
- A Python integer that does not fit the array's type, which raises
|
|
45
|
+
`OverflowError` in NumPy.
|
|
46
|
+
- Shapes of unknown rank.
|
|
47
|
+
- Indexing by arrays or masks.
|
|
48
|
+
- `einsum`, `numpy.linalg`, `numpy.fft` and the array constructors.
|
|
49
|
+
- Sums of products in the formula algebra: `n*m + 1` is `ANY`.
|
shaperules-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sofeikov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: shaperules
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: NumPy's shape and dtype rules as a small library: symbolic sizes, three honest answers, no NumPy needed.
|
|
5
|
+
Project-URL: Homepage, https://sofeikov.github.io/shaperules/
|
|
6
|
+
Project-URL: Documentation, https://sofeikov.github.io/shaperules/
|
|
7
|
+
Project-URL: Repository, https://github.com/sofeikov/shaperules
|
|
8
|
+
Project-URL: Issues, https://github.com/sofeikov/shaperules/issues
|
|
9
|
+
Author: sofeikov
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: broadcasting,dtype,numpy,shape,static-analysis,symbolic
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# shaperules
|
|
27
|
+
|
|
28
|
+
[](https://github.com/sofeikov/shaperules/actions/workflows/ci.yml)
|
|
29
|
+
[](https://pypi.org/project/shaperules/)
|
|
30
|
+

|
|
31
|
+

|
|
32
|
+
|
|
33
|
+
NumPy's shape and dtype rules as a pure-Python library with no dependencies. Give a rule the shapes of the operands and it answers with the shape of the result, without NumPy installed and without running anything.
|
|
34
|
+
|
|
35
|
+
A size is an integer, a formula over names such as `n` or `2*n+1`, or unknown. Every rule answers one of three things:
|
|
36
|
+
|
|
37
|
+
- `Known(value)`: the result, proven for every value the names can take.
|
|
38
|
+
- `Refused(refusal)`: NumPy raises, for every value the names can take.
|
|
39
|
+
- `Unknown(reason, likely)`: it depends on sizes the rule was not told. `likely` is a best reading for a caller that wants one, and is not a proof.
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
>>> import shaperules as sr
|
|
43
|
+
|
|
44
|
+
>>> sr.broadcast((8, 1, 3), (5, 3))
|
|
45
|
+
Known(value=(8, 5, 3))
|
|
46
|
+
|
|
47
|
+
>>> product = sr.matmul(("batch", "n", 64), (64, 10))
|
|
48
|
+
>>> sr.fmt_shape(product.value)
|
|
49
|
+
'(batch, n, 10)'
|
|
50
|
+
|
|
51
|
+
>>> refused = sr.matmul((2, 3), (4, 5))
|
|
52
|
+
>>> refused.refusal.kind, refused.refusal.raises
|
|
53
|
+
('matmul-core', 'ValueError')
|
|
54
|
+
|
|
55
|
+
>>> doubt = sr.broadcast(("n",), (3,))
|
|
56
|
+
>>> doubt.reason
|
|
57
|
+
'sizes at axis -1 must be equal or 1: n and 3'
|
|
58
|
+
>>> doubt.likely
|
|
59
|
+
Known(value=(3,))
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The last call is valid when `n` is 3 or 1 and raises in NumPy otherwise, so nothing is proven. A caller that must agree with NumPy exactly acts on `Known` and `Refused` and asks NumPy for the rest. A caller such as a type checker can carry on with `likely`.
|
|
64
|
+
|
|
65
|
+
Element types (dtypes) work the same way:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
>>> sr.result_type("int8", "uint8").value
|
|
69
|
+
'int16'
|
|
70
|
+
>>> sr.result_type("float32", float).value
|
|
71
|
+
'float32'
|
|
72
|
+
>>> sr.ufunc_dtype("true_divide", "int32", "int32").value
|
|
73
|
+
'float64'
|
|
74
|
+
>>> sr.ufunc_dtype("subtract", "bool", "bool").refusal.raises
|
|
75
|
+
'TypeError'
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Documentation: [sofeikov.github.io/shaperules](https://sofeikov.github.io/shaperules/)
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
pip install shaperules
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
uv add shaperules
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
It needs Python 3.11 or later and installs nothing else.
|
|
92
|
+
|
|
93
|
+
## What is covered
|
|
94
|
+
|
|
95
|
+
Version 0.1 covers these rules. Each one takes shapes or element types and answers with a shape, a size or an element type.
|
|
96
|
+
|
|
97
|
+
| Area | Rules | NumPy calls they stand for |
|
|
98
|
+
|---|---|---|
|
|
99
|
+
| Broadcasting | `broadcast`, `broadcast_pair`, `match_pair` | `np.broadcast_shapes`, and so the shape of any element-wise operation |
|
|
100
|
+
| Matrix product | `matmul` | `a @ b`, `np.matmul` |
|
|
101
|
+
| Reductions | `reduce`, `arg_reduce`, `cumulative` | `np.sum`, `np.prod`, `np.any`, `np.max`, `np.min`, `np.argmax`, `np.cumsum` and the like |
|
|
102
|
+
| Reordering axes | `transpose`, `swapaxes`, `moveaxis` | `a.T`, `np.transpose`, `np.swapaxes`, `np.moveaxis` |
|
|
103
|
+
| Adding and removing axes | `squeeze`, `expand_dims` | `np.squeeze`, `np.expand_dims` |
|
|
104
|
+
| Regrouping elements | `reshape` (with `-1`), `concatenate`, `stack` | `np.reshape`, `np.concatenate`, `np.stack` |
|
|
105
|
+
| Basic indexing | `index`, `slice_length` | `a[key]` where the key holds integers, slices, `None` and `...` |
|
|
106
|
+
| Element types | `result_type`, `can_cast`, `ufunc_dtype`, `ufunc_dtypes`, `reduce_dtype` | `np.result_type`, `np.can_cast`, the result type of every element-wise ufunc of one or two inputs, and of sixteen reductions |
|
|
107
|
+
|
|
108
|
+
Fourteen element types are covered: `bool`, `int8` to `int64`, `uint8` to `uint64`, `float16` to `float64`, `complex64` and `complex128`. A Python `int`, `float` or `complex` among the operands is promoted the way NumPy 2 promotes a Python number (NEP 50).
|
|
109
|
+
|
|
110
|
+
Each rule is described with examples in the [guide to the shape rules](https://sofeikov.github.io/shaperules/guide/shape-rules/) and the [guide to element types](https://sofeikov.github.io/shaperules/guide/dtypes/).
|
|
111
|
+
|
|
112
|
+
## How it is checked
|
|
113
|
+
|
|
114
|
+
NumPy is not needed to use shaperules. It is used by the tests, where it is the reference every answer is held against.
|
|
115
|
+
|
|
116
|
+
- **Shape rules.** [Hypothesis](https://hypothesis.readthedocs.io/) draws shapes whose sizes are formulas over a few names. The test asks the rule, then makes the real NumPy call for every assignment of the sizes 0 to 3 to those names. A `Known` answer must be NumPy's shape every time. A `Refused` answer must be an exception of the named class every time.
|
|
117
|
+
- **Element types.** Promotion is compared with `np.result_type` for every single operand, every pair, every triple of different operands, and every combination of four array types. Every cell of every ufunc table and every reduction is compared with the NumPy call it stands for.
|
|
118
|
+
- **The tables.** What a ufunc returns for each pair of element types is read from tables generated from NumPy by `tools/generate_tables.py`. A test generates them again on the NumPy under test and fails on any difference.
|
|
119
|
+
- **Versions.** CI runs the suite on NumPy 2.1, the oldest version modelled, and on the latest, and installs the wheel into an environment with no NumPy to check that it answers there.
|
|
120
|
+
- **The examples.** Every example in this README and in the documentation is run by the test suite.
|
|
121
|
+
|
|
122
|
+
The full account is in [Checked against NumPy](https://sofeikov.github.io/shaperules/internals/checked-against-numpy/).
|
|
123
|
+
|
|
124
|
+
## Status and limits
|
|
125
|
+
|
|
126
|
+
Version 0.1.0 is the first version. The following are not modelled:
|
|
127
|
+
|
|
128
|
+
- A Python integer that does not fit the array's type. `np.uint8(1) + 300` raises `OverflowError` in NumPy. That depends on a value, and shaperules sees types only.
|
|
129
|
+
- Shapes of unknown rank. A shape is a tuple with one size per axis, so the number of axes is always known.
|
|
130
|
+
- Indexing by arrays or boolean masks.
|
|
131
|
+
- `einsum`, `numpy.linalg`, `numpy.fft` and the functions that build arrays.
|
|
132
|
+
- Sums of products in the formula algebra. `n*m + 1` has no form there and is read as an unknown size.
|
|
133
|
+
- Element types outside the fourteen, such as `datetime64`, `longdouble` and `object`. A rule given one answers `Unknown`.
|
|
134
|
+
|
|
135
|
+
A refusal's message is written by shaperules and is not NumPy's message. The list with details is in [Limitations and roadmap](https://sofeikov.github.io/shaperules/internals/limitations/).
|
|
136
|
+
|
|
137
|
+
## Where it comes from
|
|
138
|
+
|
|
139
|
+
The rules were extracted from [numty](https://github.com/sofeikov/numty), a mypy plugin that checks NumPy shapes statically, so that clients other than a type checker can use them. numty imports its size algebra and its dtype promotion from shaperules. A second intended client is the lazy layer of [hotkernels](https://github.com/sofeikov/hotkernels), which needs the shape and element type of a NumPy call at run time without computing it. hotkernels does not use shaperules yet.
|
|
140
|
+
|
|
141
|
+
## Documentation
|
|
142
|
+
|
|
143
|
+
- [Get started](https://sofeikov.github.io/shaperules/get-started/)
|
|
144
|
+
- [Sizes](https://sofeikov.github.io/shaperules/guide/sizes/): integers, names, formulas and the unknown size
|
|
145
|
+
- [The three answers](https://sofeikov.github.io/shaperules/guide/answers/)
|
|
146
|
+
- [Writing a client](https://sofeikov.github.io/shaperules/guide/clients/): a run-time client and a static one
|
|
147
|
+
- [API reference](https://sofeikov.github.io/shaperules/reference/api/) and [the list of refusals](https://sofeikov.github.io/shaperules/reference/refusals/)
|
|
148
|
+
- [Design](https://sofeikov.github.io/shaperules/internals/design/)
|
|
149
|
+
- [Development](https://sofeikov.github.io/shaperules/contributing/)
|
|
150
|
+
|
|
151
|
+
## Licence
|
|
152
|
+
|
|
153
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# shaperules
|
|
2
|
+
|
|
3
|
+
[](https://github.com/sofeikov/shaperules/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/shaperules/)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
NumPy's shape and dtype rules as a pure-Python library with no dependencies. Give a rule the shapes of the operands and it answers with the shape of the result, without NumPy installed and without running anything.
|
|
9
|
+
|
|
10
|
+
A size is an integer, a formula over names such as `n` or `2*n+1`, or unknown. Every rule answers one of three things:
|
|
11
|
+
|
|
12
|
+
- `Known(value)`: the result, proven for every value the names can take.
|
|
13
|
+
- `Refused(refusal)`: NumPy raises, for every value the names can take.
|
|
14
|
+
- `Unknown(reason, likely)`: it depends on sizes the rule was not told. `likely` is a best reading for a caller that wants one, and is not a proof.
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
>>> import shaperules as sr
|
|
18
|
+
|
|
19
|
+
>>> sr.broadcast((8, 1, 3), (5, 3))
|
|
20
|
+
Known(value=(8, 5, 3))
|
|
21
|
+
|
|
22
|
+
>>> product = sr.matmul(("batch", "n", 64), (64, 10))
|
|
23
|
+
>>> sr.fmt_shape(product.value)
|
|
24
|
+
'(batch, n, 10)'
|
|
25
|
+
|
|
26
|
+
>>> refused = sr.matmul((2, 3), (4, 5))
|
|
27
|
+
>>> refused.refusal.kind, refused.refusal.raises
|
|
28
|
+
('matmul-core', 'ValueError')
|
|
29
|
+
|
|
30
|
+
>>> doubt = sr.broadcast(("n",), (3,))
|
|
31
|
+
>>> doubt.reason
|
|
32
|
+
'sizes at axis -1 must be equal or 1: n and 3'
|
|
33
|
+
>>> doubt.likely
|
|
34
|
+
Known(value=(3,))
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The last call is valid when `n` is 3 or 1 and raises in NumPy otherwise, so nothing is proven. A caller that must agree with NumPy exactly acts on `Known` and `Refused` and asks NumPy for the rest. A caller such as a type checker can carry on with `likely`.
|
|
39
|
+
|
|
40
|
+
Element types (dtypes) work the same way:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
>>> sr.result_type("int8", "uint8").value
|
|
44
|
+
'int16'
|
|
45
|
+
>>> sr.result_type("float32", float).value
|
|
46
|
+
'float32'
|
|
47
|
+
>>> sr.ufunc_dtype("true_divide", "int32", "int32").value
|
|
48
|
+
'float64'
|
|
49
|
+
>>> sr.ufunc_dtype("subtract", "bool", "bool").refusal.raises
|
|
50
|
+
'TypeError'
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Documentation: [sofeikov.github.io/shaperules](https://sofeikov.github.io/shaperules/)
|
|
55
|
+
|
|
56
|
+
## Install
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
pip install shaperules
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
uv add shaperules
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
It needs Python 3.11 or later and installs nothing else.
|
|
67
|
+
|
|
68
|
+
## What is covered
|
|
69
|
+
|
|
70
|
+
Version 0.1 covers these rules. Each one takes shapes or element types and answers with a shape, a size or an element type.
|
|
71
|
+
|
|
72
|
+
| Area | Rules | NumPy calls they stand for |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| Broadcasting | `broadcast`, `broadcast_pair`, `match_pair` | `np.broadcast_shapes`, and so the shape of any element-wise operation |
|
|
75
|
+
| Matrix product | `matmul` | `a @ b`, `np.matmul` |
|
|
76
|
+
| Reductions | `reduce`, `arg_reduce`, `cumulative` | `np.sum`, `np.prod`, `np.any`, `np.max`, `np.min`, `np.argmax`, `np.cumsum` and the like |
|
|
77
|
+
| Reordering axes | `transpose`, `swapaxes`, `moveaxis` | `a.T`, `np.transpose`, `np.swapaxes`, `np.moveaxis` |
|
|
78
|
+
| Adding and removing axes | `squeeze`, `expand_dims` | `np.squeeze`, `np.expand_dims` |
|
|
79
|
+
| Regrouping elements | `reshape` (with `-1`), `concatenate`, `stack` | `np.reshape`, `np.concatenate`, `np.stack` |
|
|
80
|
+
| Basic indexing | `index`, `slice_length` | `a[key]` where the key holds integers, slices, `None` and `...` |
|
|
81
|
+
| Element types | `result_type`, `can_cast`, `ufunc_dtype`, `ufunc_dtypes`, `reduce_dtype` | `np.result_type`, `np.can_cast`, the result type of every element-wise ufunc of one or two inputs, and of sixteen reductions |
|
|
82
|
+
|
|
83
|
+
Fourteen element types are covered: `bool`, `int8` to `int64`, `uint8` to `uint64`, `float16` to `float64`, `complex64` and `complex128`. A Python `int`, `float` or `complex` among the operands is promoted the way NumPy 2 promotes a Python number (NEP 50).
|
|
84
|
+
|
|
85
|
+
Each rule is described with examples in the [guide to the shape rules](https://sofeikov.github.io/shaperules/guide/shape-rules/) and the [guide to element types](https://sofeikov.github.io/shaperules/guide/dtypes/).
|
|
86
|
+
|
|
87
|
+
## How it is checked
|
|
88
|
+
|
|
89
|
+
NumPy is not needed to use shaperules. It is used by the tests, where it is the reference every answer is held against.
|
|
90
|
+
|
|
91
|
+
- **Shape rules.** [Hypothesis](https://hypothesis.readthedocs.io/) draws shapes whose sizes are formulas over a few names. The test asks the rule, then makes the real NumPy call for every assignment of the sizes 0 to 3 to those names. A `Known` answer must be NumPy's shape every time. A `Refused` answer must be an exception of the named class every time.
|
|
92
|
+
- **Element types.** Promotion is compared with `np.result_type` for every single operand, every pair, every triple of different operands, and every combination of four array types. Every cell of every ufunc table and every reduction is compared with the NumPy call it stands for.
|
|
93
|
+
- **The tables.** What a ufunc returns for each pair of element types is read from tables generated from NumPy by `tools/generate_tables.py`. A test generates them again on the NumPy under test and fails on any difference.
|
|
94
|
+
- **Versions.** CI runs the suite on NumPy 2.1, the oldest version modelled, and on the latest, and installs the wheel into an environment with no NumPy to check that it answers there.
|
|
95
|
+
- **The examples.** Every example in this README and in the documentation is run by the test suite.
|
|
96
|
+
|
|
97
|
+
The full account is in [Checked against NumPy](https://sofeikov.github.io/shaperules/internals/checked-against-numpy/).
|
|
98
|
+
|
|
99
|
+
## Status and limits
|
|
100
|
+
|
|
101
|
+
Version 0.1.0 is the first version. The following are not modelled:
|
|
102
|
+
|
|
103
|
+
- A Python integer that does not fit the array's type. `np.uint8(1) + 300` raises `OverflowError` in NumPy. That depends on a value, and shaperules sees types only.
|
|
104
|
+
- Shapes of unknown rank. A shape is a tuple with one size per axis, so the number of axes is always known.
|
|
105
|
+
- Indexing by arrays or boolean masks.
|
|
106
|
+
- `einsum`, `numpy.linalg`, `numpy.fft` and the functions that build arrays.
|
|
107
|
+
- Sums of products in the formula algebra. `n*m + 1` has no form there and is read as an unknown size.
|
|
108
|
+
- Element types outside the fourteen, such as `datetime64`, `longdouble` and `object`. A rule given one answers `Unknown`.
|
|
109
|
+
|
|
110
|
+
A refusal's message is written by shaperules and is not NumPy's message. The list with details is in [Limitations and roadmap](https://sofeikov.github.io/shaperules/internals/limitations/).
|
|
111
|
+
|
|
112
|
+
## Where it comes from
|
|
113
|
+
|
|
114
|
+
The rules were extracted from [numty](https://github.com/sofeikov/numty), a mypy plugin that checks NumPy shapes statically, so that clients other than a type checker can use them. numty imports its size algebra and its dtype promotion from shaperules. A second intended client is the lazy layer of [hotkernels](https://github.com/sofeikov/hotkernels), which needs the shape and element type of a NumPy call at run time without computing it. hotkernels does not use shaperules yet.
|
|
115
|
+
|
|
116
|
+
## Documentation
|
|
117
|
+
|
|
118
|
+
- [Get started](https://sofeikov.github.io/shaperules/get-started/)
|
|
119
|
+
- [Sizes](https://sofeikov.github.io/shaperules/guide/sizes/): integers, names, formulas and the unknown size
|
|
120
|
+
- [The three answers](https://sofeikov.github.io/shaperules/guide/answers/)
|
|
121
|
+
- [Writing a client](https://sofeikov.github.io/shaperules/guide/clients/): a run-time client and a static one
|
|
122
|
+
- [API reference](https://sofeikov.github.io/shaperules/reference/api/) and [the list of refusals](https://sofeikov.github.io/shaperules/reference/refusals/)
|
|
123
|
+
- [Design](https://sofeikov.github.io/shaperules/internals/design/)
|
|
124
|
+
- [Development](https://sofeikov.github.io/shaperules/contributing/)
|
|
125
|
+
|
|
126
|
+
## Licence
|
|
127
|
+
|
|
128
|
+
MIT. See [LICENSE](LICENSE).
|