conditional
Conditionally enter a context manager
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 on this page — 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
conditional on PyPI
pip
pip install conditionaluv
uv add conditionalpoetry
poetry add conditionalInstalling 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 the current Python release (>=3.5) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 41 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 233,637/month — #9,036 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: conditional-2.2-py3-none-any.whl
Keywords: conditional, context manager, contextmanager, with, async, async with, enter, exit
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
coverage-conditional-pluginA coverage.py plugin that applies conditional…
permissive · top 15,000 on PyPI
enum-compatConditionally installs enum34 on Python…
permissive · top 5,000 on PyPI
pytest-skip-markersA pytest plugin that provides skip markers to…
permissive · top 15,000 on PyPI
pytest-enablerA pytest plugin that conditionally enables…
permissive · top 15,000 on PyPI
codemod-toxParses and modifies tox.ini configuration…
permissive · top 15,000 on PyPI
rest_conditionProvides logical operators (And, Or, Not) to…
permissive · top 15,000 on PyPI
stubsProvides a context-manager syntax for…
permissive · top 15,000 on PyPI
contextvarsProvides the PEP 567 contextvars module for…
permissive · top 5,000 on PyPI
stopitProvides context managers and decorators to…
permissive · top 15,000 on PyPI
mypy-strict-kwargsA mypy plugin that enforces…
permissive · top 15,000 on PyPI