fgpyo icon indicating copy to clipboard operation
fgpyo copied to clipboard

Add `require`

Open msto opened this issue 1 year ago • 1 comments

assert should be avoided outside unit tests.

The Google style guide requires this:

Do not use assert statements in place of conditionals or validating preconditions. They must not be critical to the application logic. A litmus test would be that the assert could be removed without breaking the code. assert conditionals are not guaranteed to be evaluated. For pytest based tests, assert is 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.
    """
    ...

msto avatar Jun 06 '24 15:06 msto

Hey @msto, Can I pick this issue?

harsha-mangena avatar Jun 06 '24 15:06 harsha-mangena