How to Assert an Invariant After a Complex Transformation in Python

Use assert to verify that a multi-step transformation preserves a mathematical invariant, catching regressions early.

Easy Python 3.8+ Aug 9, 2026 Errors & debugging 13 views 0 copies

Python code

23 lines
Python 3.8+
def transform_value(value):
    """Apply several transformations to a value."""
    doubled = value * 2
    shifted = doubled + 10
    normalized = shifted / 2
    return int(normalized)

def assert_invariant(value):
    """Assert that the transformation preserves a key invariant."""
    original = value
    transformed = transform_value(value)
    # Invariant: original * 1 + 5 equals transformed
    expected = original + 5
    assert transformed == expected, f"Invariant violated: {transformed} != {expected}"
    return transformed

if __name__ == "__main__":
    test_value = 7
    result = assert_invariant(test_value)
    print(f"Invariant holds for {test_value} -> {result}")
    # Test with a negative number
    result2 = assert_invariant(-3)
    print(f"Invariant holds for -3 -> {result2}")

Output

stdout
Invariant holds for 7 -> 12
Invariant holds for -3 -> 2

How it works

assert evaluates the boolean expression and raises AssertionError with the message if false. This pattern wraps a transformation and a known mathematical relationship into a single check. By enforcing the invariant inside a function, callers get immediate feedback when the logic drifts. The message interpolation makes failures actionable, showing the unexpected value and the expected one. Assertions can be disabled with -O, so they suit debug and test scenarios rather than production validation.

Common mistakes

  • Relying on assert for critical production checks where the interpreter may run with -O
  • Using integer division or rounding that silently breaks the invariant
  • Writing ambiguous error messages without the actual values
  • Ignoring that float arithmetic can cause tiny precision mismatches

Variations

  1. Use a custom exception, e.g. `raise ValueError`, for production invariant checks
  2. Extract the expected value computation into a separate helper for clearer tests

Real-world use cases

  • Verifying that an encryption/decryption round-trip reconstructs the original plaintext.
  • Sanity-checking that a file transformation preserves the row count after an ETL pipeline step.
  • Ensuring a payment calculation's net amount equals gross minus fees in financial code.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Errors & debugging

Related tutorials and quizzes for this topic.