Skip to main content

PyPi Package Build Status Coverage Report Documentation Status

alternative

Keep a trusted Python implementation alongside candidate rewrites, test them with pytest, and choose which one normal callers use. alternative supports functions and methods, preserves their type signatures, and has no runtime dependencies. It requires Python 3.10 or newer.

Use it when optimizing an existing function or comparing algorithms with the same public contract. You provide the correctness tests and benchmarks; the library registers implementations and keeps the selected default stable.

Install

pip install alternative pytest pytest-benchmark

Only alternative is needed at runtime. The other packages provide the testing and timing tools used below.

Register and call implementations

import alternative


@alternative.reference
def total(values: list[int]) -> int:
    """Keep a straightforward implementation as the reference."""
    result = 0
    for value in values:
        result += value
    return result


@total.add
def total_builtin(values: list[int]) -> int:
    """Register a candidate with the same contract."""
    return sum(values)


assert total([1, 2, 3]) == 6
assert total_builtin([1, 2, 3]) == 6

total(...) uses the reference until you select an explicit default. total_builtin(...) always calls that candidate directly.

Test and benchmark

In the same file, or in a test module that imports total, add:

import pytest


@total.pytest_parametrize()
@pytest.mark.parametrize(("values", "expected"), [([], 0), ([1, -2, 3], 2)])
def test_expected_totals(implementation, values, expected):
    """Check every implementation against independent expected results."""
    assert implementation(values) == expected


@total.pytest_parametrize_pairs()
def test_equivalent_totals(reference, implementation):
    """Compare each candidate with the reference."""
    values = [1, 2, 3]
    assert implementation(values) == reference(values)


@total.pytest_parametrize()
def test_total_benchmark(benchmark, implementation):
    """Time each implementation on the same input."""
    assert benchmark(implementation, list(range(1_000))) == 499_500

Save the combined example as test_totals.py and run pytest test_totals.py. The helpers supply implementation and reference; they are callable test parameters, not fixtures you need to write.

After checking correctness and measuring representative inputs, edit the candidate registration to @total.add(default=True) and run a fresh process. Normal calls to total(...) will then use that candidate. The library does not choose a winner automatically.

Register every candidate before calling the alternatives object or applying a pytest helper. Those operations freeze registration, as do reading .callable or .implementations and calling .measure(...). This prevents collected tests from silently missing later imports. Set ALTERNATIVE_DEBUG=1 to include the earlier registration or inspection location in relevant errors.

Next steps

Metadata

Release files for alternative 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alternative 0.4.0
File Size Uploaded Provenance Actions
alternative-0.4.0.tar.gz 142.6 kB With provenance Details for alternative-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for alternative 0.4.0
File Interpreter ABI Platform Actions
alternative-0.4.0-py3-none-any.whl Python 3 none any Details for alternative-0.4.0-py3-none-any.whl

Total release size: 156.2 kB

Release files / alternative-0.4.0.tar.gz

Download URL alternative-0.4.0.tar.gz
Size 142.6 kB
Tags Source
SHA-256 checksum
How to use checksums
11f685c872cc71583ee31a50818584cf98c955b8b0aab64959f711854ed7d946
BLAKE2b-256 checksum
How to use checksums
22a5855a1b8b2739f94631ed83c56a0bdb4642aa8bc6664a92b44b1c0050e980
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / alternative-0.4.0-py3-none-any.whl

Download URL alternative-0.4.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f76fc29fc00324ed9bf1583dfa306ff18e36719045984f3a5810046af200ec51
BLAKE2b-256 checksum
How to use checksums
41383755c8b3146f133a3122ae824ca289e18b542f88aca84aec0fb41e8c9261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Security for alternative 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for alternative-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page