Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Safe assert for Python that can be used together with optimised mode

Project description

safe-assert

wemake.services Build Status Coverage Python Version wemake-python-styleguide

Allows users to write 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_number(numbers: List[int]) -> List[int]:
    safe_assert(all(num >= 0 for num in numbers), 'found negative')
    return sorted(numbers)

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.

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.

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for safe-assert, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size safe_assert-0.1.0-py3-none-any.whl (4.2 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size safe-assert-0.1.0.tar.gz (4.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page