Convert Python assertions to type annotations and back
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file annassert-0.1.1.tar.gz.
File metadata
- Download URL: annassert-0.1.1.tar.gz
- Upload date:
- Size: 63.3 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51a74706124d4cb1b1eb5792211d01fe1dc5d4eb0a57414ad9068f59d669fa6a
|
|
| MD5 |
83f231823b7c53dd37c20882da445b1d
|
|
| BLAKE2b-256 |
e0955f6f0ab99a51119810afc95cba7e55ac21e05e73cbedbec82a683e047670
|
File details
Details for the file annassert-0.1.1-py3-none-any.whl.
File metadata
- Download URL: annassert-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8faa919caae828e85b4ab463e2d05a874600cfdd578c88b104847b32c338241a
|
|
| MD5 |
a5cb3fd8ca0fb2bea3e58ac283e87cf0
|
|
| BLAKE2b-256 |
b2431ced27964c7a9b73e042dbf7ac81b93846584c002729732ab65fbd07e1ac
|