Skip to main content

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

Project description

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.

Project details


Download files

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

Source Distribution

safe-assert-0.4.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

safe_assert-0.4.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file safe-assert-0.4.0.tar.gz.

File metadata

  • Download URL: safe-assert-0.4.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.0 Darwin/18.7.0

File hashes

Hashes for safe-assert-0.4.0.tar.gz
Algorithm Hash digest
SHA256 88c6bd08c73d8273db3426c1dc8a3623c190bfe347f337dab1cf0c586351469a
MD5 ffbe190806d98694a36f42c430d90dec
BLAKE2b-256 32f8c1a45a09a2a0f152a69c4fb6366e9419cc4792736e8030e419df0175a5ae

See more details on using hashes here.

File details

Details for the file safe_assert-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: safe_assert-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.0 Darwin/18.7.0

File hashes

Hashes for safe_assert-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0de0fff39046d74ccd511af5b4682fe9f5660ff657d732450eb1c6c443e87258
MD5 c12c86383fe06bc2c7d5f692da3a3c05
BLAKE2b-256 87c7e50e64b0b0339eb6802860bc2a285b454bfffd85baa310fa51b2ac30b590

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page