_util.py 813 B

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