Coverage for src/extratools_core/typing.py: 69%
160 statements
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-27 20:50 -0700
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-27 20:50 -0700
1from abc import abstractmethod
2from collections.abc import Iterable, Sequence
3from typing import IO, Any, Protocol, Self, runtime_checkable
6@runtime_checkable
7class Comparable(Protocol): # noqa: PLW1641
8 """
9 Based on https://github.com/python/typing/issues/59
10 """
12 @abstractmethod
13 def __eq__(self, other: object, /) -> bool:
14 ...
16 @abstractmethod
17 def __lt__(self, other: Self, /) -> bool:
18 ...
20 def __gt__(self, other: Self, /) -> bool:
21 return (not self < other) and self != other
23 def __le__(self, other: Self, /) -> bool:
24 return self < other or self == other
26 def __ge__(self, other: Self, /) -> bool:
27 return (not self < other)
30@runtime_checkable
31class PurePathLike(Comparable, Protocol):
32 @property
33 @abstractmethod
34 def parts(self) -> Sequence[str]:
35 ...
37 @property
38 @abstractmethod
39 def parent(self) -> Self:
40 ...
42 @property
43 @abstractmethod
44 def parents(self) -> Sequence[Self]:
45 ...
47 @property
48 @abstractmethod
49 def name(self) -> str:
50 ...
52 @property
53 @abstractmethod
54 def suffix(self) -> str:
55 ...
57 @property
58 @abstractmethod
59 def suffixes(self) -> Sequence[str]:
60 ...
62 @property
63 @abstractmethod
64 def stem(self) -> str:
65 ...
67 @abstractmethod
68 def is_absolute(self) -> bool:
69 ...
71 @abstractmethod
72 def is_relative_to(self, other: Self, /) -> bool:
73 ...
75 @abstractmethod
76 def relative_to(self, other: Self, /) -> Any:
77 ...
79 @abstractmethod
80 def joinpath(self, *segments: Any) -> Self:
81 ...
83 @abstractmethod
84 def full_match(self, pattern: str) -> bool:
85 ...
87 @abstractmethod
88 def match(self, path_pattern: str) -> bool:
89 ...
91 @abstractmethod
92 def with_name(self, name: str) -> Self:
93 ...
95 @abstractmethod
96 def with_suffix(self, suffix: str) -> Self:
97 ...
99 @abstractmethod
100 def with_stem(self, stem: str) -> Self:
101 ...
103 @abstractmethod
104 def with_segments(self, *segments: Any) -> Self:
105 ...
107 def __truediv__(self, key: Any, /) -> Self:
108 return self.with_segments(self, key)
110 @abstractmethod
111 def __rtruediv__(self, key: Any, /) -> Any:
112 ...
114 @abstractmethod
115 def __fspath__(self) -> str:
116 ...
119@runtime_checkable
120class PathLike(PurePathLike, Protocol):
121 @classmethod
122 @abstractmethod
123 def from_uri(cls, uri: str) -> Self:
124 ...
126 @abstractmethod
127 def as_uri(self) -> str:
128 ...
130 @abstractmethod
131 def stat(self) -> Any:
132 ...
134 @abstractmethod
135 def open(self) -> IO[Any]:
136 ...
138 @abstractmethod
139 def read_bytes(self) -> bytes:
140 ...
142 @abstractmethod
143 def write_bytes(self, data: bytes) -> Any:
144 ...
146 @abstractmethod
147 def read_text(self) -> str:
148 ...
150 @abstractmethod
151 def write_text(self, data: str) -> Any:
152 ...
154 @abstractmethod
155 def iterdir(self) -> Iterable[Self]:
156 ...
158 @abstractmethod
159 def glob(self, pattern: str) -> Iterable[Self]:
160 ...
162 @abstractmethod
163 def rglob(self, pattern: str) -> Iterable[Self]:
164 ...
166 @abstractmethod
167 def walk(self, top_down: bool = True) -> Iterable[tuple[Self, Sequence[str], Sequence[str]]]:
168 ...
170 @abstractmethod
171 def absolute(self) -> Self:
172 ...
174 @abstractmethod
175 def resolve(self) -> Self:
176 ...
178 @abstractmethod
179 def exists(self) -> bool:
180 ...
182 @abstractmethod
183 def is_dir(self) -> bool:
184 ...
186 @abstractmethod
187 def is_file(self) -> bool:
188 ...
190 @abstractmethod
191 def samefile(self, other_path: str | Self) -> bool:
192 ...
194 @abstractmethod
195 def touch(self, *, exist_ok: bool = True) -> None:
196 ...
198 @abstractmethod
199 def mkdir(self, *, parents: bool = False, exist_ok: bool = False) -> None:
200 ...
202 @abstractmethod
203 def unlink(self, *, missing_ok=False) -> None:
204 ...
206 @abstractmethod
207 def rmdir(self) -> None:
208 ...
210 @abstractmethod
211 def rename(self, target: Self) -> Self:
212 ...
214 @abstractmethod
215 def replace(self, target: Self) -> Self:
216 ...