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