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

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: str) -> 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 

104@runtime_checkable 

105class PathLike(PurePathLike, Protocol): 

106 @classmethod 

107 @abstractmethod 

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

109 ... 

110 

111 @abstractmethod 

112 def as_uri(self) -> str: 

113 ... 

114 

115 @abstractmethod 

116 def stat(self) -> Any: 

117 ... 

118 

119 @abstractmethod 

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

121 ... 

122 

123 @abstractmethod 

124 def read_bytes(self) -> bytes: 

125 ... 

126 

127 @abstractmethod 

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

129 ... 

130 

131 @abstractmethod 

132 def read_text(self) -> str: 

133 ... 

134 

135 @abstractmethod 

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

137 ... 

138 

139 @abstractmethod 

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

141 ... 

142 

143 @abstractmethod 

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

145 ... 

146 

147 @abstractmethod 

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

149 ... 

150 

151 @abstractmethod 

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

153 ... 

154 

155 @abstractmethod 

156 def absolute(self) -> Self: 

157 ... 

158 

159 @abstractmethod 

160 def resolve(self) -> Self: 

161 ... 

162 

163 @abstractmethod 

164 def exists(self) -> bool: 

165 ... 

166 

167 @abstractmethod 

168 def is_dir(self) -> bool: 

169 ... 

170 

171 @abstractmethod 

172 def is_file(self) -> bool: 

173 ... 

174 

175 @abstractmethod 

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

177 ... 

178 

179 @abstractmethod 

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

181 ... 

182 

183 @abstractmethod 

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

185 ... 

186 

187 @abstractmethod 

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

189 ... 

190 

191 @abstractmethod 

192 def rmdir(self) -> None: 

193 ... 

194 

195 @abstractmethod 

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

197 ... 

198 

199 @abstractmethod 

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

201 ...