alternative 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.
- alternative-0.1.0/.github/workflows/ci.yml +75 -0
- alternative-0.1.0/.github/workflows/publish.yml +27 -0
- alternative-0.1.0/.gitignore +194 -0
- alternative-0.1.0/.python-version +1 -0
- alternative-0.1.0/AGENTS.md +5 -0
- alternative-0.1.0/PKG-INFO +50 -0
- alternative-0.1.0/README.md +41 -0
- alternative-0.1.0/alternative.py +404 -0
- alternative-0.1.0/examples/test_benchmark.py +19 -0
- alternative-0.1.0/examples/test_equivalence.py +26 -0
- alternative-0.1.0/examples/test_measure.py +45 -0
- alternative-0.1.0/pyproject.toml +39 -0
- alternative-0.1.0/test_alternative.py +249 -0
- alternative-0.1.0/test_get_callsite_path.py +39 -0
- alternative-0.1.0/test_pytest_util.py +22 -0
- alternative-0.1.0/uv.lock +156 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
- master
|
|
8
|
+
pull_request:
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
tests:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
env:
|
|
14
|
+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
|
15
|
+
strategy:
|
|
16
|
+
fail-fast: false
|
|
17
|
+
matrix:
|
|
18
|
+
python-version:
|
|
19
|
+
- "3.12"
|
|
20
|
+
- "3.13"
|
|
21
|
+
- "3.14"
|
|
22
|
+
steps:
|
|
23
|
+
- name: Check out repository
|
|
24
|
+
uses: actions/checkout@v4
|
|
25
|
+
|
|
26
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
27
|
+
uses: actions/setup-python@v5
|
|
28
|
+
with:
|
|
29
|
+
python-version: ${{ matrix.python-version }}
|
|
30
|
+
|
|
31
|
+
- name: Install uv
|
|
32
|
+
uses: astral-sh/setup-uv@v1
|
|
33
|
+
|
|
34
|
+
- name: Install dependencies
|
|
35
|
+
run: |
|
|
36
|
+
uv sync --dev
|
|
37
|
+
uv tool install ruff
|
|
38
|
+
uv tool install pyrefly
|
|
39
|
+
uv tool install mypy
|
|
40
|
+
|
|
41
|
+
- name: Ruff format check
|
|
42
|
+
run: uv run --dev ruff format --check --diff .
|
|
43
|
+
continue-on-error: true
|
|
44
|
+
|
|
45
|
+
- name: Type check
|
|
46
|
+
run: |
|
|
47
|
+
uv run --dev pyrefly check .
|
|
48
|
+
uv run --dev mypy .
|
|
49
|
+
continue-on-error: true
|
|
50
|
+
|
|
51
|
+
- name: Run tests
|
|
52
|
+
run: |
|
|
53
|
+
# Some GitHub-hosted runners export PYTEST_DISABLE_PLUGIN_AUTOLOAD=1.
|
|
54
|
+
# Unset it so pytest can discover pytest-cov and pytest-benchmark via entry points,
|
|
55
|
+
# which are required by the addopts configured in pyproject.toml.
|
|
56
|
+
unset PYTEST_DISABLE_PLUGIN_AUTOLOAD
|
|
57
|
+
uv run --dev pytest -vv --cov=alternative --cov-report=xml --junitxml=test-results.xml
|
|
58
|
+
|
|
59
|
+
- name: Upload coverage to Codecov
|
|
60
|
+
if: always() && env.CODECOV_TOKEN != ''
|
|
61
|
+
uses: codecov/codecov-action@v5
|
|
62
|
+
with:
|
|
63
|
+
token: ${{ env.CODECOV_TOKEN }}
|
|
64
|
+
files: ./coverage.xml
|
|
65
|
+
fail_ci_if_error: true
|
|
66
|
+
|
|
67
|
+
- name: Upload test results to Codecov
|
|
68
|
+
if: always() && env.CODECOV_TOKEN != ''
|
|
69
|
+
uses: codecov/codecov-action@v5
|
|
70
|
+
with:
|
|
71
|
+
token: ${{ env.CODECOV_TOKEN }}
|
|
72
|
+
files: ./test-results.xml
|
|
73
|
+
report_type: test_results
|
|
74
|
+
fail_ci_if_error: true
|
|
75
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build-and-publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
environment:
|
|
11
|
+
name: pypi
|
|
12
|
+
url: https://pypi.org/p/alternative
|
|
13
|
+
permissions:
|
|
14
|
+
id-token: write
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- name: Set up Python
|
|
18
|
+
uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: '3.x'
|
|
21
|
+
- name: Build package
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip build
|
|
24
|
+
python -m build
|
|
25
|
+
# the publishing uses PyPI's trusted publishing, so configured on pypi instead of using secrets
|
|
26
|
+
- name: Publish to PyPI
|
|
27
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
.idea/
|
|
169
|
+
|
|
170
|
+
# Abstra
|
|
171
|
+
# Abstra is an AI-powered process automation framework.
|
|
172
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
173
|
+
# Learn more at https://abstra.io/docs
|
|
174
|
+
.abstra/
|
|
175
|
+
|
|
176
|
+
# Visual Studio Code
|
|
177
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
178
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
179
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
180
|
+
# you could uncomment the following to ignore the enitre vscode folder
|
|
181
|
+
# .vscode/
|
|
182
|
+
|
|
183
|
+
# Ruff stuff:
|
|
184
|
+
.ruff_cache/
|
|
185
|
+
|
|
186
|
+
# PyPI configuration file
|
|
187
|
+
.pypirc
|
|
188
|
+
|
|
189
|
+
# Cursor
|
|
190
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
191
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
192
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
193
|
+
.cursorignore
|
|
194
|
+
.cursorindexingignore
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
The repository defines testing via GitHub actions. When contributing:
|
|
2
|
+
|
|
3
|
+
* Run the same steps locally that the workflow performs. This includes running `uv run --dev pytest`, `uv run --dev ruff format --check .`, `pyrefly check .`, and `mypy .`.
|
|
4
|
+
* Ensure code is formatted with `ruff format` before committing.
|
|
5
|
+
* Name tests and functions in `snake_case` and give them triple-quoted docstrings similar to the current codebase.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: alternative
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A library to help with the management of alternative implementations of functions
|
|
5
|
+
Author-email: Oliver Bristow <evilumbrella+github@gmail.com>
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: pytest>=8.3.5
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
[](https://pypi.org/project/alternative/) [](https://github.com/Code0x58/alternative/actions/workflows/ci.yml) [](https://codecov.io/gh/Code0x58/alternative)
|
|
11
|
+
|
|
12
|
+
This is a library to help with the management of alternative implementations of functions, as you may have when
|
|
13
|
+
optimising.
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import alternative
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@alternative.reference
|
|
20
|
+
def constant_number():
|
|
21
|
+
return 1
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@constant_number.add(default=True)
|
|
25
|
+
def alternative_constant_number():
|
|
26
|
+
return 2
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@constant_number.add
|
|
30
|
+
def unused_alternative_constant_number():
|
|
31
|
+
return 3
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# the reference implementation is substituted with the default implementation - if no default was specified, the reference would have been used
|
|
35
|
+
assert constant_number() == 2
|
|
36
|
+
# alternative implementations still act like themselves
|
|
37
|
+
assert unused_alternative_constant_number() == 3
|
|
38
|
+
|
|
39
|
+
# TODO: mash in measure + benchmark + equality tests from examples
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The library tries to avoid unpleasant surprises that may come from import orders or maintenance issues:
|
|
43
|
+
* the implementation that is used cannot be changed once it has been used
|
|
44
|
+
* implementations cannot be added once they have been inspected: reduces the changes of tests only covering a subset of
|
|
45
|
+
|
|
46
|
+
Running with `ALTERNATIVE_DEBUG=1` as an environment variable will result in the library recording which lines were
|
|
47
|
+
responsible for critical state changes like the point that a default implementation was selected or the implementations
|
|
48
|
+
were inspected. These are then used in error messages which can be used to resolve stateful issues.
|
|
49
|
+
|
|
50
|
+
For how this can tie in with equivalence checks and benchmarking in pytest, see the examples directory.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[](https://pypi.org/project/alternative/) [](https://github.com/Code0x58/alternative/actions/workflows/ci.yml) [](https://codecov.io/gh/Code0x58/alternative)
|
|
2
|
+
|
|
3
|
+
This is a library to help with the management of alternative implementations of functions, as you may have when
|
|
4
|
+
optimising.
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
import alternative
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@alternative.reference
|
|
11
|
+
def constant_number():
|
|
12
|
+
return 1
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@constant_number.add(default=True)
|
|
16
|
+
def alternative_constant_number():
|
|
17
|
+
return 2
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@constant_number.add
|
|
21
|
+
def unused_alternative_constant_number():
|
|
22
|
+
return 3
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# the reference implementation is substituted with the default implementation - if no default was specified, the reference would have been used
|
|
26
|
+
assert constant_number() == 2
|
|
27
|
+
# alternative implementations still act like themselves
|
|
28
|
+
assert unused_alternative_constant_number() == 3
|
|
29
|
+
|
|
30
|
+
# TODO: mash in measure + benchmark + equality tests from examples
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The library tries to avoid unpleasant surprises that may come from import orders or maintenance issues:
|
|
34
|
+
* the implementation that is used cannot be changed once it has been used
|
|
35
|
+
* implementations cannot be added once they have been inspected: reduces the changes of tests only covering a subset of
|
|
36
|
+
|
|
37
|
+
Running with `ALTERNATIVE_DEBUG=1` as an environment variable will result in the library recording which lines were
|
|
38
|
+
responsible for critical state changes like the point that a default implementation was selected or the implementations
|
|
39
|
+
were inspected. These are then used in error messages which can be used to resolve stateful issues.
|
|
40
|
+
|
|
41
|
+
For how this can tie in with equivalence checks and benchmarking in pytest, see the examples directory.
|