Registry / testing / safe-assert

safe-assert

JSON →
library0.5.0pypypi✓ verified 84d ago

Provides a drop-in replacement for Python's assert statement that remains active even when Python is run with -O (optimized mode). Currently at version 0.5.0, supports Python 3.9-3.12, and is released irregularly.

pip install safe-assert
INSTALL
IMPORT
SIG · SAFE-ASSERT
S
safe-assert
testingpythonv0.5.0
Install
1.8s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

safe_assert
from safe_assert import safe_assert
import safe_assert
safe_assert is a function, not a module; importing the module directly gives AttributeError when calling safe_assert()
AssertionError
from safe_assert import AssertionError
Custom AssertionError is raised (subclass of builtins.AssertionError)

Run this script normally or with python -O; safe_assert will still raise an error.

from safe_assert import safe_assert x = 0 safe_assert(x != 0, "x should not be zero") # raises safe_assert.AssertionError if False, even with python -O
Debug
Known issues
gotchasafe_assert is a function, not a replacement for the assert statement syntax. You cannot write `safe_assert x == 1`; you must call it as `safe_assert(x == 1)`.
fix
Use function call syntax: safe_assert(condition, message)
affects: all
deprecatedVersion 0.5.0 changes the return annotation from NoReturn to None. This is a typing-only change; runtime behavior is unaffected.
fix
If you rely on the return type being NoReturn, update your type hints to None.
affects: 0.5.0
breakingPython 3.7 and 3.8 support dropped in version 0.5.0.
fix
Upgrade to Python 3.9+ or pin safe-assert to <0.5.0.
affects: >=0.5.0
Errors
Common errors & fixes
AttributeError: module 'safe_assert' has no attribute 'safe_assert'
Importing the module directly, then trying to call safe_assert.safe_assert() but the function is not exported at module level as an attribute.
fix
Use 'from safe_assert import safe_assert' instead of 'import safe_assert'.
TypeError: 'module' object is not callable
Using 'import safe_assert' and then 'safe_assert(condition, message)' attempts to call the module itself, which is not callable.
fix
Use 'from safe_assert import safe_assert' and then call 'safe_assert(condition, message)'.
Upgrade
Version history
0.5.0latest on PyPI · released Mar 29, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
safe-assert — pip install safe-assert · libregistry