Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Allows users to write composable assert
s that are not stripped away in optimized mode.
safe_assert
is a function, it can be easily composed with other functionspip 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 conjunction 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.
MIT.
FAQs
Safe assert for Python that can be used together with optimised mode
We found that safe-assert demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.