Coverage for src/extratools_core/typing.py: 69%

160 statements  

« prev     ^ index     » next       coverage.py v7.8.1, created at 2025-05-23 01:16 -0700

1from abc import abstractmethod 

2from collections.abc import Iterable, Sequence 

3from typing import IO, Any, Protocol, Self, runtime_checkable 

4 

5 

6@runtime_checkable 

7class Comparable(Protocol): # noqa: PLW1641 

8 """ 

9 Based on https://github.com/python/typing/issues/59 

10 """ 

11 

12 @abstractmethod 

13 def __eq__(self, other: object, /) -> bool: 

14 ... 

15 

16 @abstractmethod 

17 def __lt__(self, other: Self, /) -> bool: 

18 ... 

19 

20 def __gt__(self, other: Self, /) -> bool: 

21 return (not self < other) and self != other 

22 

23 def __le__(self, other: Self, /) -> bool: 

24 return self < other or self == other 

25 

26 def __ge__(self, other: Self, /) -> bool: 

27 return (not self < other) 

28 

29 

30@runtime_checkable 

31class PurePathLike(Comparable, Protocol): 

32 @property 

33 @abstractmethod 

34 def parts(self) -> Sequence[str]: 

35 ... 

36 

37 @property 

38 @abstractmethod 

39 def parent(self) -> Self: 

40 ... 

41 

42 @property 

43 @abstractmethod 

44 def parents(self) -> Sequence[Self]: 

45 ... 

46 

47 @property 

48 @abstractmethod 

49 def name(self) -> str: 

50 ... 

51 

52 @property 

53 @abstractmethod 

54 def suffix(self) -> str: 

55 ... 

56 

57 @property 

58 @abstractmethod 

59 def suffixes(self) -> Sequence[str]: 

60 ... 

61 

62 @property 

63 @abstractmethod 

64 def stem(self) -> str: 

65 ... 

66 

67 @abstractmethod 

68 def is_absolute(self) -> bool: 

69 ... 

70 

71 @abstractmethod 

72 def is_relative_to(self, other: Self, /) -> bool: 

73 ... 

74 

75 @abstractmethod 

76 def relative_to(self, other: Self, /) -> Any: 

77 ... 

78 

79 @abstractmethod 

80 def joinpath(self, *segments: Any) -> Self: 

81 ... 

82 

83 @abstractmethod 

84 def full_match(self, pattern: str) -> bool: 

85 ... 

86 

87 @abstractmethod 

88 def match(self, path_pattern: str) -> bool: 

89 ... 

90 

91 @abstractmethod 

92 def with_name(self, name: str) -> Self: 

93 ... 

94 

95 @abstractmethod 

96 def with_suffix(self, suffix: str) -> Self: 

97 ... 

98 

99 @abstractmethod 

100 def with_stem(self, stem: str) -> Self: 

101 ... 

102 

103 @abstractmethod 

104 def with_segments(self, *segments: Any) -> Self: 

105 ... 

106 

107 def __truediv__(self, key: Any, /) -> Self: 

108 return self.with_segments(self, key) 

109 

110 @abstractmethod 

111 def __rtruediv__(self, key: Any, /) -> Any: 

112 ... 

113 

114 @abstractmethod 

115 def __fspath__(self) -> str: 

116 ... 

117 

118 

119@runtime_checkable 

120class PathLike(PurePathLike, Protocol): 

121 @classmethod 

122 @abstractmethod 

123 def from_uri(cls, uri: str) -> Self: 

124 ... 

125 

126 @abstractmethod 

127 def as_uri(self) -> str: 

128 ... 

129 

130 @abstractmethod 

131 def stat(self) -> Any: 

132 ... 

133 

134 @abstractmethod 

135 def open(self) -> IO[Any]: 

136 ... 

137 

138 @abstractmethod 

139 def read_bytes(self) -> bytes: 

140 ... 

141 

142 @abstractmethod 

143 def write_bytes(self, data: bytes) -> Any: 

144 ... 

145 

146 @abstractmethod 

147 def read_text(self) -> str: 

148 ... 

149 

150 @abstractmethod 

151 def write_text(self, data: str) -> Any: 

152 ... 

153 

154 @abstractmethod 

155 def iterdir(self) -> Iterable[Self]: 

156 ... 

157 

158 @abstractmethod 

159 def glob(self, pattern: str) -> Iterable[Self]: 

160 ... 

161 

162 @abstractmethod 

163 def rglob(self, pattern: str) -> Iterable[Self]: 

164 ... 

165 

166 @abstractmethod 

167 def walk(self, top_down: bool = True) -> Iterable[tuple[Self, Sequence[str], Sequence[str]]]: 

168 ... 

169 

170 @abstractmethod 

171 def absolute(self) -> Self: 

172 ... 

173 

174 @abstractmethod 

175 def resolve(self) -> Self: 

176 ... 

177 

178 @abstractmethod 

179 def exists(self) -> bool: 

180 ... 

181 

182 @abstractmethod 

183 def is_dir(self) -> bool: 

184 ... 

185 

186 @abstractmethod 

187 def is_file(self) -> bool: 

188 ... 

189 

190 @abstractmethod 

191 def samefile(self, other_path: str | Self) -> bool: 

192 ... 

193 

194 @abstractmethod 

195 def touch(self, *, exist_ok: bool = True) -> None: 

196 ... 

197 

198 @abstractmethod 

199 def mkdir(self, *, parents: bool = False, exist_ok: bool = False) -> None: 

200 ... 

201 

202 @abstractmethod 

203 def unlink(self, *, missing_ok=False) -> None: 

204 ... 

205 

206 @abstractmethod 

207 def rmdir(self) -> None: 

208 ... 

209 

210 @abstractmethod 

211 def rename(self, target: Self) -> Self: 

212 ... 

213 

214 @abstractmethod 

215 def replace(self, target: Self) -> Self: 

216 ...