_util.py 684 B

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