Safe assert for Python that can be used together with optimised mode
Project description
safe-assert
Allows users to write composable assert
s 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 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
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
File details
Details for the file safe-assert-0.2.0.tar.gz
.
File metadata
- Download URL: safe-assert-0.2.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.7.7 Darwin/18.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac010a2544a993c3b17f60e54e14e7c82c64a8d4ced810fbbef05b6efc855931 |
|
MD5 | 5231b365a095b3884149c3a53c1ca5ec |
|
BLAKE2b-256 | 25ac1321abe0a2397019bb2b2d225c5af9479f82d306983c79070d88564990ca |
File details
Details for the file safe_assert-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: safe_assert-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.7.7 Darwin/18.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f8138562945aa1e102159b64d5db33c32133b7a55de2dcda45d018018eb4c84 |
|
MD5 | 90806dc090ae49e4329c70d5ddd28cab |
|
BLAKE2b-256 | be4b4336537c0eecea67b743bc744ff1a5200af970e1da9f5751f9f2123b4869 |