Skip to content

Latest commit

 

History

History
61 lines (41 loc) · 1.46 KB

index.rst

File metadata and controls

61 lines (41 loc) · 1.46 KB

conditional |version| -- Conditional Context

.. toctree::
   :maxdepth: 2
.. module:: conditional

The :class:`~conditional.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()

Asynchronous context managers are supported since version 2.0:

async with conditional(CONDITION, ASYNCCONTEXTMANAGER()):
    BODY()

The :class:`~conditional.conditional` context manager ships with type annotations. Type checkers and IDEs can use this information to implement type safety and auto completion.

API Documentation

.. function:: conditional(condition, contextmanager)

    Wrap a context manager and enter it only if condition is true.

Indices and Tables