skillfed

conditional

Conditionally enter a context manager

conditional v2.2 233.6K downloads/30d#9,036 on PyPI10
Permissive license BSD-2-Clause Active released

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 conditional

uv

uv add conditional

poetry

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 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Typing :: Typed

Tags

conditional context manageroptional context managercontext manager if conditionconditional with statementasync context manager conditionalskip context manager based on conditioncontext manager wrapper
context-managerasync-supporttyped

More Software Development packages