conditional
Conditionally enter a context manager
Package Contents
- conditional(condition, contextmanager)
Enter contextmanager only if condition is true.
Overview
The conditional context manager comes handy when you always want to execute a with-block but only conditionally want to apply its context manager.
If you find yourself writing code like this:
if CONDITION:
with CONTEXTMANAGER():
BODY()
else:
BODY()
Consider replacing it with:
with conditional(CONDITION, CONTEXTMANAGER()):
BODY()
Async
conditional supports asynchronous context managers:
async with conditional(CONDITION, ASYNCCONTEXTMANAGER()):
BODY()
Typing
The package ships with type annotations. Type checkers and IDEs can use this information to implement type safety and auto completion.
Examples
Say we want to ignore signals when a pager application is in the foreground, but not otherwise:
from conditional import conditional
with conditional(has_pager(cmd), ignoresignals()):
os.system(cmd)
Documentation
For further details please refer to the API Documentation.
Changelog
2.2 - 2026-07-04
Fix the release date. [stefan]
2.1 - 2026-07-04
Remove deprecated license classifier. [stefan]
Upgrade GitHub workflow. [stefan]
Upgrade .readthedocs.yaml. [stefan]
Upgrade sphinx and sphinx-rtd-theme in docs extra. [stefan]
2.0 - 2024-05-14
Support asynchronous context managers. Thanks to Dawid Wolski. [stefan]
Require Python >= 3.5. [stefan]
1.5 - 2023-09-14
Add type annotations to the context manager. [stefan]
Update tox.ini for latest tox. [stefan]
Add GitHub CI workflow. [stefan]
Add .readthedocs.yaml file. [stefan]
Pin sphinx and sphinx-rtd-theme versions in docs extra. [stefan]
Add mypy extra which installs mypy. [stefan]
1.4 - 2022-03-09
Add Python 3.8-3.10 to tox.ini. Remove old Python versions. [stefan]
Replace deprecated python setup.py test in tox.ini. [stefan]
Remove deprecated test_suite and tests_require from setup.py. [stefan]
Move metadata to setup.cfg and add a pyproject.toml file. [stefan]
Include tests in sdist but not in wheel. [stefan]
1.3 - 2019-01-28
Add MANIFEST.in. [stefan]
Release as wheel. [stefan]
1.2 - 2017-02-05
Support Python 2.6-3.6 without 2to3. [stefan]
Add a LICENSE file. [stefan]
1.1 - 2014-04-19
Remove setuptools from install_requires because it isn’t. [stefan]
1.0 - 2012-05-16
Initial release. [stefan]
Metadata
Release files for conditional 2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| conditional-2.2.tar.gz | 8.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| conditional-2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / conditional-2.2.tar.gz
| Download URL | conditional-2.2.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e8c3b1a534d7deb36284b3dccc5074f9b2543608b666011cac85c5aac88b5601
|
|
BLAKE2b-256 checksum How to use checksums |
2a197596264a6ce33c759d9540a429ac02001ee4345ccd47ffcd5d4463d36e71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.2
|
Release files / conditional-2.2-py3-none-any.whl
| Download URL | conditional-2.2-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2a1010fcd70a155bdf5345c866b02ae5f162477f8f40a57acd681ba41fc2c41c
|
|
BLAKE2b-256 checksum How to use checksums |
36b31e1fe61e98fe98701fa860ee1d1aad7615adaf8ef54ccc0de5ea70eaa0eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.2
|