{"id":9618,"library":"conditional","title":"Conditional Context Manager","description":"The `conditional` library provides a `Conditional` context manager that executes code within a `with` block only when a specified boolean condition is true. It simplifies conditional execution of code sections, particularly useful when dealing with other context managers or resource allocation that should only happen under certain circumstances. As of version 2.0, it is actively maintained with a low release cadence.","status":"active","version":"2.0","language":"en","source_language":"en","source_url":"https://github.com/stefanholek/conditional","tags":["context-manager","conditional-execution","utility"],"install":[{"cmd":"pip install conditional","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Conditional","correct":"from conditional import Conditional"}],"quickstart":{"code":"from conditional import Conditional\n\n# Example 1: Condition is True, block executes\nprint(\"Before true conditional block\")\nwith Conditional(True):\n    print(\"Inside true conditional block\")\n    with open(\"temp_file.txt\", \"w\") as f:\n        f.write(\"This was written conditionally.\\n\")\nprint(\"After true conditional block\")\n\n# Example 2: Condition is False, block does not execute\nprint(\"\\nBefore false conditional block\")\nwith Conditional(False):\n    print(\"Inside false conditional block (should not see this)\")\n    # The file will not be opened or written to\n    with open(\"another_temp_file.txt\", \"w\") as f:\n        f.write(\"This was NOT written conditionally.\\n\")\nprint(\"After false conditional block\")\n\n# Cleanup (optional)\nimport os\nif os.path.exists(\"temp_file.txt\"):\n    os.remove(\"temp_file.txt\")\nif os.path.exists(\"another_temp_file.txt\"):\n    os.remove(\"another_temp_file.txt\")","lang":"python","description":"This example demonstrates how to use `Conditional` to wrap a block of code, including other context managers, based on a boolean condition."},"warnings":[{"fix":"Ensure the `condition` argument is explicitly a `bool`. If you previously passed a callable, evaluate it *before* passing the result to `Conditional`: `with Conditional(my_callable()):` instead of `with Conditional(my_callable):`.","message":"In `conditional` version 2.0 and later, the `condition` argument to `Conditional` MUST be a boolean. In versions prior to 2.0, it accepted a callable, which it would then execute to determine the condition. Passing a non-boolean (e.g., an integer, string, or callable) in v2.0+ will raise a `TypeError`.","severity":"breaking","affected_versions":"2.0+"},{"fix":"Use it as `with Conditional(my_condition): with MyActualContextManager(): ...` if you want to conditionally enter `MyActualContextManager`. For simple conditional execution without another context manager, you might also consider a standard `if my_condition: ...` block.","message":"The `Conditional` context manager takes a *boolean condition*, not another context manager. It serves to conditionally enter its `with` block, where you would then place your actual context-managed resource or code. It does not make an existing context manager conditional itself.","severity":"gotcha","affected_versions":"all"},{"fix":"If certain operations must always occur regardless of the condition, place them outside the `Conditional` block. For complex conditional logic, a standard `if/else` statement might offer more clarity and control over divergent paths.","message":"Be aware that if the `condition` evaluates to `False`, the code within the `with Conditional(False):` block will not execute at all. This means any side effects (e.g., file writes, database connections, object instantiations) within that block will not occur. Ensure that any necessary setup or cleanup logic is handled appropriately outside or around the conditional block.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Explicitly convert your condition to a boolean: `with Conditional(bool(0)):` or `with Conditional(my_condition == 5):`.","cause":"In `conditional` v2.0+, the `condition` argument must be a boolean. You likely passed an integer (or other non-boolean type) expecting it to be treated as a callable or implicitly converted.","error":"TypeError: 'int' object is not callable"},{"fix":"Always provide a boolean condition when instantiating `Conditional`: `with Conditional(True): ...` or `my_condition = False; with Conditional(my_condition): ...`.","cause":"You are attempting to instantiate `Conditional` without providing the required `condition` argument.","error":"TypeError: __init__ missing 1 required positional argument: 'condition'"},{"fix":"Ensure you are using `Conditional` correctly within a `with` statement: `with Conditional(True): # your code here`.","cause":"You are trying to use `Conditional` as a regular class instance or function call, rather than as a context manager with a `with` statement.","error":"AttributeError: 'Conditional' object has no attribute '__enter__'"}]}