{"id":4352,"library":"ensure","title":"Ensure","description":"Ensure is a Python library offering a set of simple, literate assertion helpers for validating conditions. It allows writing expressive, concise, and readable Pythonic code for validation, inspired by JavaScript assertion libraries. Beyond testing, 'ensure' can be used for production code validation as it doesn't rely on the 'assert' statement (which can be disabled with the -O flag). The current version is 1.0.4, and it is actively maintained.","status":"active","version":"1.0.4","language":"en","source_language":"en","source_url":"https://github.com/kislyuk/ensure","tags":["BDD","testing","assertions","literate","validation","type-checking"],"install":[{"cmd":"pip install ensure","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary entry point for assertion chaining.","symbol":"ensure","correct":"from ensure import ensure"},{"note":"Used for validations that raise a custom exception or a specific type, rather than an EnsureError.","symbol":"check","correct":"from ensure import check"},{"note":"Decorator for enforcing function signature type annotations (Python 3 only).","symbol":"ensure_annotations","correct":"from ensure import ensure_annotations"}],"quickstart":{"code":"from ensure import ensure, check\n\n# Basic assertions\nensure(1).is_an(int)\nensure(\"hello\").is_a(str).has_length(5)\nensure([1, 2, 3]).contains(1).and_also.does_not_contain(4)\n\n# Chained assertions\nensure({\"a\": 1, \"b\": 2}).has_key(\"a\").whose_value.is_an(int)\n\n# Function call assertions\nensure(int).called_with(\"1100101\", base=2).returns(101)\nensure(dict).called_with(1, 2).raises(TypeError)\n\n# Using 'check' for custom exception handling\ntry:\n    check(1).is_a(float).or_raise(ValueError, \"Value must be a float, got {value}\")\nexcept ValueError as e:\n    print(f\"Caught expected error: {e}\")\n\n# Type annotation enforcement (Python 3)\nfrom ensure import ensure_annotations, EnsureError\n\n@ensure_annotations\ndef add_numbers(a: int, b: int) -> int:\n    return a + b\n\nprint(f\"2 + 3 = {add_numbers(2, 3)}\")\n\n# This would typically raise an EnsureError\ntry:\n    add_numbers(\"2\", 3)\nexcept EnsureError as e:\n    print(f\"Caught expected annotation error: {e}\")\n","lang":"python","description":"This quickstart demonstrates core `ensure` functionalities: basic type and value assertions, chained property checks, function call validation (including expected return values and raised exceptions), and the use of `check` for custom exception types. It also shows how to apply `ensure_annotations` for runtime type checking in functions."},"warnings":[{"fix":"Avoid using `@ensure_annotations` on functions that are passed directly to `multiprocessing` pools or other serialization mechanisms. Consider performing validation at the entry point of the worker process instead, or use alternative validation methods for multiprocessing-sensitive code.","message":"When using `ensure_annotations` with multiprocessing, you might encounter `PicklingError` due to issues with pickling decorated functions. This is a known limitation when dealing with annotation enforcement across process boundaries.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware of this distinction when choosing between `assert` and `ensure`. If certain runtime checks are performance-critical and should only run in debug environments, use Python's native `assert`.","message":"Unlike Python's built-in `assert` statement, `ensure` is designed to be production-safe and will not have its checks disabled when Python is run with the `-O` (optimization) flag. If you intend for checks to be removable in optimized builds, `ensure` is not the right tool for those specific checks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to `ensure` version 0.8.1 or later. This issue was resolved in versions 0.8.1 and 0.8.2. Ensure your Python environment is compatible with the library's `requires_python >=3.6`.","message":"Older versions (pre-0.8.1) of `ensure` had compatibility issues with the `collections.abc` module due to changes in Python's standard library, particularly impacting type checking for abstract base classes. This could lead to `ImportError` or unexpected behavior.","severity":"breaking","affected_versions":"<0.8.1"},{"fix":"Update to `ensure` version 1.0.4 or newer to ensure full compatibility and avoid deprecation warnings with Python 3.12.","message":"Python 3.12 deprecated `unittest.TestCase.assertRaisesRegexp` in favor of `unittest.TestCase.assertRaisesRegex`. Although `ensure` updated its internal usage in v1.0.4, older versions of `ensure` might experience compatibility issues or warnings when run with Python 3.12, particularly in tests that internally rely on the `unittest` module's regex assertion methods.","severity":"gotcha","affected_versions":"<1.0.4 (when used with Python 3.12)"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}