Skip to main content

AnnAssert

annassert (Annotated Assertions) is a Python library and CLI tool that turns assertions into annotations, and vice versa.

Go from here:

def repeat(s, n):
    """Repeat string s n times."""
    assert isinstance(s, str) and len(s) > 0
    assert isinstance(n, int) and n >= 0 and n <= 100
    return s * n

To there:

def repeat(s: Annotated[str, MinLen(1)], n: Annotated[int, Ge(0), Le(100)]):
    """Repeat string s n times."""
    assert len(s) >= 1
    assert n >= 0 and n <= 100
    return s * n

And enjoy your type-checker superpowers!

How to use

Install with pip or ruff, then:

# by default, adds annotations AND assertions
annassert path/to/your/code.py

# diff mode
annassert --diff path/to/your/code.py

# check mode
annassert --check path/to/your/code.py

Supported conversions

Assert clause Annotation Notes
x > v Annotated[T, Gt(v)]
x >= v Annotated[T, Ge(v)]
x < v Annotated[T, Lt(v)]
x <= v Annotated[T, Le(v)]
len(x) >= v Annotated[T, MinLen(v)]
len(x) <= v Annotated[T, MaxLen(v)]
x % v == 0 Annotated[T, MultipleOf(v)]
x.ndim == n Shaped[T, "_ … _"] (n dims) jaxtyping
x.shape[i] == v Shaped[T, "… v …"] jaxtyping, integer v only
(type checker) Float[T, s], Int[T, s], … jaxtyping dtype — no runtime assert
(type checker) x: T base type — no runtime assert

Note: We have only partial support for named jaxtyping axes (e.g. "batch seq d_model"). They are translated to weaker assertions (assert x.ndims == 4)

Why ?

This project was created during the Apart Research Software security hackathon, as part of a broader effort to drastically improve the quality of python research code.

Metadata

Release files for annassert 0.1.1

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

Source distribution (sdist)

Source distribution for annassert 0.1.1
File Size Uploaded Provenance Actions
annassert-0.1.1.tar.gz 63.3 kB Without provenance Details for annassert-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 76.1 kB

Release files / annassert-0.1.1.tar.gz

Download URL annassert-0.1.1.tar.gz
Size 63.3 kB
Tags Source
SHA-256 checksum
How to use checksums
51a74706124d4cb1b1eb5792211d01fe1dc5d4eb0a57414ad9068f59d669fa6a
BLAKE2b-256 checksum
How to use checksums
e0955f6f0ab99a51119810afc95cba7e55ac21e05e73cbedbec82a683e047670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / annassert-0.1.1-py3-none-any.whl

Download URL annassert-0.1.1-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8faa919caae828e85b4ab463e2d05a874600cfdd578c88b104847b32c338241a
BLAKE2b-256 checksum
How to use checksums
b2431ced27964c7a9b73e042dbf7ac81b93846584c002729732ab65fbd07e1ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for annassert 0.1.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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