Skip to content

Use optional_dependency for transports #455

Open

Description

@tomkane-dls

PR #454 introduces a context manager for optional imports. Once the connections branch is merged in to main this should be expanded to the optional transport imports as well.

In utils.py

@contextmanager
def optional_dependency(*names: str) -> Generator[None, None, None]:
    """Skip the imports in the block if one of the given packages isn't installed.

    Example::

        with optional_dependency("httpx"):
            from .http_connection import HTTPConnection as HTTPConnection

    Args:
        names: The packages that the block may be missing

    Raises:
        ModuleNotFoundError: If a module other than the given packages is missing

    """
    try:
        yield
    except ModuleNotFoundError as e:
        if e.name is None or e.name.split(".")[0] not in names:
            raise

Activity

  1. self-assigned this
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions