Coverage for src/extratools_core/typing.py: 70%
149 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-07 06:52 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-07 06:52 -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: str) -> 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 ...
104@runtime_checkable
105class PathLike(PurePathLike, Protocol):
106 @classmethod
107 @abstractmethod
108 def from_uri(cls, uri: str) -> Self:
109 ...
111 @abstractmethod
112 def as_uri(self) -> str:
113 ...
115 @abstractmethod
116 def stat(self) -> Any:
117 ...
119 @abstractmethod
120 def open(self) -> IO[Any]:
121 ...
123 @abstractmethod
124 def read_bytes(self) -> bytes:
125 ...
127 @abstractmethod
128 def write_bytes(self, data: bytes) -> Any:
129 ...
131 @abstractmethod
132 def read_text(self) -> str:
133 ...
135 @abstractmethod
136 def write_text(self, data: str) -> Any:
137 ...
139 @abstractmethod
140 def iterdir(self) -> Iterable[Self]:
141 ...
143 @abstractmethod
144 def glob(self, pattern: str) -> Iterable[Self]:
145 ...
147 @abstractmethod
148 def rglob(self, pattern: str) -> Iterable[Self]:
149 ...
151 @abstractmethod
152 def walk(self, top_down: bool = True) -> Iterable[tuple[Self, Sequence[str], Sequence[str]]]:
153 ...
155 @abstractmethod
156 def absolute(self) -> Self:
157 ...
159 @abstractmethod
160 def resolve(self) -> Self:
161 ...
163 @abstractmethod
164 def exists(self) -> bool:
165 ...
167 @abstractmethod
168 def is_dir(self) -> bool:
169 ...
171 @abstractmethod
172 def is_file(self) -> bool:
173 ...
175 @abstractmethod
176 def samefile(self, other_path: str | Self) -> bool:
177 ...
179 @abstractmethod
180 def touch(self, *, exist_ok: bool = True) -> None:
181 ...
183 @abstractmethod
184 def mkdir(self, *, parents: bool = False, exist_ok: bool = False) -> None:
185 ...
187 @abstractmethod
188 def unlink(self, *, missing_ok=False) -> None:
189 ...
191 @abstractmethod
192 def rmdir(self) -> None:
193 ...
195 @abstractmethod
196 def rename(self, target: Self) -> Self:
197 ...
199 @abstractmethod
200 def replace(self, target: Self) -> Self:
201 ...