Skip to content
Safe and composable assert for Python that can be used together with optimised mode
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
safe_assert Initial release Oct 26, 2019
.editorconfig Initial release Oct 26, 2019
.gitignore Initial release Oct 26, 2019
.travis.yml Initial release Oct 26, 2019 Initial release Oct 26, 2019 Initial release Oct 26, 2019
LICENSE Initial release Oct 26, 2019 Update Nov 11, 2019
poetry.lock Initial release Oct 26, 2019
pyproject.toml Initial release Oct 26, 2019
setup.cfg Initial release Oct 26, 2019

safe-assert Build Status Coverage Python Version wemake-python-styleguide

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


  • 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


pip install safe-assert


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 conjuction with dry-python projects.


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

See docs to learn more.



You can’t perform that action at this time.