conditional
Conditionally enter a context manager
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires Python >= 3.5.
- 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 · maintenance · safety
BSD-2-Clause (permissive) — BSD-2-Clause is permissive; you can use this package freely in commercial or private projects with minimal restrictions.
last release 2026-07-04 (41 days) · last repo commit 2026-08-07 · 10 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 233,637 downloads/mo, #9,036 on PyPI
Alternatives
Verify before relying
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()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 on it.
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
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.
Requires Python >= 3.5.
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()
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 |
| Classifiers | Development Status :: 5 - Production/StableIntended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Typing :: Typed |
Evidence: conditional-2.2-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “conditional context manager”
- conditionalConditionally apply a context manager to a code block based on a…
- async-timeoutProvides an asyncio-compatible timeout context manager for applying…
- stubsProvides a context-manager syntax for temporarily replacing function…
Give your agent the search over MCP, or paste the wish link into any chat.
More Software Development packages
Provides backported and experimental type hints for Python 3.9+, allowing use of newer typing features on older Python versions and enabling early experimentation with type system PEPs before they enter the standard library.
NumPy provides an N-dimensional array object and a comprehensive suite of mathematical, linear algebra, Fourier transform, and random number functions for scientific computing in Python.
FastAPI is a Python web framework for building REST APIs using type hints, with automatic request validation, serialization, and interactive API documentation.
Provides a way to document function parameters, class attributes, return types, and variables inline using Python's `Annotated` type hint syntax instead of traditional docstrings.
Typer builds command-line applications from Python functions using type hints, automatically generating help text, argument parsing, and shell completion.
Install it if you are building CLIs in Python.
Distlib provides low-level packaging utilities for building, distributing, and managing Python software—including metadata handling, version specifiers, wheel support, script installation, and dependency resolution.
See also coverage-conditional-plugin · enum-compat · pytest-skip-markers · pytest-enabler · codemod-tox · rest_condition · stubs · contextvars · stopit · mypy-strict-kwargs