fgpyo
fgpyo copied to clipboard
Add `require`
assert should be avoided outside unit tests.
The Google style guide requires this:
Do not use
assertstatements in place of conditionals or validating preconditions. They must not be critical to the application logic. A litmus test would be that theassertcould be removed without breaking the code.assertconditionals are not guaranteed to be evaluated. For pytest based tests,assertis okay and expected to verify expectations.
The primary reason to avoid assert statements is that they are intended for debugging and can be disabled.
Per slack discussion, it would be convenient to have a require() function in fgpyo that acts like assert but cannot be disabled.
def require(
condition: bool,
message: str | None = None,
error: Exception = RequirementError,
) -> None:
"""
Require a condition to be met.
Args:
condition: The test condition.
message: The error message to report.
error: The exception class to raise.
Raises:
`RequirementError`: if `condition` is False. (The `Exception` class raised may be overridden by the `error` argument.
"""
...
Hey @msto, Can I pick this issue?