util.py 571 B

12345678910111213141516171819202122
  1. """General utility methods"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import annotations
  5. import inspect
  6. from collections.abc import Callable
  7. from typing import Any
  8. from jupyter_core.utils import ensure_async, run_sync
  9. __all__ = ["ensure_async", "run_sync", "run_hook"]
  10. async def run_hook(hook: Callable[..., Any] | None, **kwargs: Any) -> None:
  11. """Run a hook callback."""
  12. if hook is None:
  13. return
  14. res = hook(**kwargs)
  15. if inspect.isawaitable(res):
  16. await res