--- id: conditional version: "2.2" license: BSD-2-Clause license_treatment: permissive maintenance: active --- # conditional — Conditionally enter a context manager License: permissive · Maintenance: active · Downloads: 233.6K/mo ## What it is and what it does conditional is a lightweight context manager that wraps another context manager and enters it only when a condition is true, while always executing the body of the with-block. Instead of writing separate if/else branches with duplicated code—one branch with the context manager and one without—you pass a boolean condition and the context manager to conditional, and it handles the logic. The package supports both synchronous and asynchronous context managers, includes full type annotations for IDE and type-checker support, and has no external dependencies. It is stable (Development Status 5), actively maintained, and suitable for any code path where you want to conditionally apply resource management, signal handling, or other context-dependent setup without code duplication. Use it for: - Conditionally suppress signals or exceptions in a code block depending on runtime state. - Apply a database transaction context manager only when needed, executing the same code path either way. - Optionally enable timing or profiling instrumentation around a block based on a debug flag. - Conditionally acquire a lock or semaphore depending on whether concurrent access is expected. - Skip file-locking or temporary-directory setup in certain execution modes while keeping the main logic unified. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Conditionally apply a context manager to a code block based on a boolean condition, eliminating the need to duplicate the block's body in if/else branches. Yes. This is a stable, actively maintained, zero-dependency utility that solves a genuine code-organization problem. If you write context managers and find yourself duplicating code blocks to conditionally apply them, conditional eliminates that duplication cleanly. The permissive license and type annotations add no friction. ## Install pip install conditional uv add conditional poetry add conditional ## Installing conditional Before you install: Low friction: pure Python wheel with no runtime dependencies. Active maintenance with a recent release (41 days ago) and an up-to-date repository. License in practice: BSD-2-Clause is permissive; you can use this package freely in commercial or private projects with minimal restrictions. Quickstart: from conditional import conditional with conditional(some_condition, my_context_manager()): # body executes regardless; context manager applied only if some_condition is True do_work() Requires Python >= 3.5. ## Package facts - License: BSD-2-Clause (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 233.6K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags conditional context manager, optional context manager, context manager if condition, conditional with statement, async context manager conditional, skip context manager based on condition, context manager wrapper, context-manager, async-support, typed [View on SkillFed](https://skillfed.io/packages/conditional) · [View on PyPI](https://pypi.org/project/conditional/)