_util.py 823 B

1234567891011121314151617181920212223242526272829303132
  1. from __future__ import annotations
  2. import os
  3. from pathlib import Path
  4. from typing import Any, NoReturn
  5. from ._typing import TypeGuard
  6. def is_path(f: Any) -> TypeGuard[bytes | str | Path]:
  7. return isinstance(f, (bytes, str, Path))
  8. def is_directory(f: Any) -> TypeGuard[bytes | str | Path]:
  9. """Checks if an object is a string, and that it points to a directory."""
  10. return is_path(f) and os.path.isdir(f)
  11. class DeferredError:
  12. def __init__(self, ex: BaseException):
  13. self.ex = ex
  14. def __getattr__(self, elt: str) -> NoReturn:
  15. raise self.ex
  16. @staticmethod
  17. def new(ex: BaseException) -> Any:
  18. """
  19. Creates an object that raises the wrapped exception ``ex`` when used,
  20. and casts it to :py:obj:`~typing.Any` type.
  21. """
  22. return DeferredError(ex)