Skip to main content

safe-assert

wemake.services test codecov Python Version wemake-python-styleguide

Allows users to write composable asserts that are not stripped away in optimized mode.

Features

  • Single simple, pythonic, fast, tested, typed, documented function. That's it!
  • Because safe_assert is a function, it can be easily composed with other functions
  • Fully typed with annotations and checked with mypy, PEP561 compatible

Installation

pip install safe-assert

Examples

The usage is identical to assert keyword, but a function:

from safe_assert import safe_assert

def sort_positive_numbers(numbers: List[int]) -> List[int]:
    safe_assert(all(num >= 0 for num in numbers), 'found negative')
    return sorted(numbers)

sort_positive_numbers([1, 2, 3])  # => will work
sort_positive_numbers([-1, 2, 3])
# => will fail in runtime with `AssertionError`

How is it different from regular assert? The major one is that it would not be stripped away with -O flag. So, it still allows to write declarative checks that are safe in production.

The second one is that you can compose it as any other regular function. Useful in conjunction with dry-python projects.

Internals

How does it work internally? It internally raises AssertionError that is also used by the assert keyword itself.

See docs to learn more.

License

MIT.

Release files for safe-assert 0.5.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 safe-assert 0.5.0
File Size Uploaded
safe_assert-0.5.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for safe-assert 0.5.0
File Interpreter ABI Platform
safe_assert-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.0 kB

Release files / safe_assert-0.5.0.tar.gz

Download URL safe_assert-0.5.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3d04d92d28ec6885246c8ed068dfca4109e4dbfb7c12bc1d3f9d825a86907cc9
BLAKE2b-256 checksum
How to use checksums
e5bba61f3ff476b4448532978d828a030c8f9bf81b4e04179dadc98df512c0bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.1 CPython/3.11.5 Darwin/23.0.0

Release files / safe_assert-0.5.0-py3-none-any.whl

Download URL safe_assert-0.5.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aec1348bf6baac240d263b1c69eb0c00c0d9dc90c9d73c76cb9ad30d98ea8fd1
BLAKE2b-256 checksum
How to use checksums
f0c5096a78bca1285c92384adf4d6517cfbcc1e52ce156583b962ca7fa93f2ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.1 CPython/3.11.5 Darwin/23.0.0

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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