1515from pathlib ._os import magic_open , ensure_distinct_paths , ensure_different_files , copyfileobj
1616from pathlib import PurePath , Path
1717from typing import (
18- Any , BinaryIO , Callable , Generator , Iterator , Literal , Optional , Protocol , Sequence , TypeVar ,
18+ Any , BinaryIO , Callable , Generator , Iterator , Literal , Optional , Protocol , Self , Sequence , TypeVar ,
1919 runtime_checkable ,
2020)
2121
22- _JP = TypeVar ("_JP" , bound = "_JoinablePath" )
23- _RP = TypeVar ("_RP" , bound = "_ReadablePath" )
2422_WP = TypeVar ("_WP" , bound = "_WritablePath" )
2523
2624
@@ -84,7 +82,7 @@ def parser(self) -> _PathParser:
8482 raise NotImplementedError
8583
8684 @abstractmethod
87- def with_segments (self : _JP , * pathsegments : str ) -> _JP :
85+ def with_segments (self , * pathsegments : str ) -> Self :
8886 """Construct a new path object from any number of path-like objects.
8987 Subclasses may override this method to customize how new path objects
9088 are created from methods like `iterdir()`.
@@ -136,7 +134,7 @@ def stem(self) -> str:
136134 """The final path component, minus its last suffix."""
137135 return self .parser .splitext (self .name )[0 ]
138136
139- def with_name (self : _JP , name : str ) -> _JP :
137+ def with_name (self , name : str ) -> Self :
140138 """Return a new path with the file name changed."""
141139 split = self .parser .split
142140 if split (name )[0 ]:
@@ -145,7 +143,7 @@ def with_name(self: _JP, name: str) -> _JP:
145143 path = path .removesuffix (split (path )[1 ]) + name
146144 return self .with_segments (path )
147145
148- def with_stem (self : _JP , stem : str ) -> _JP :
146+ def with_stem (self , stem : str ) -> Self :
149147 """Return a new path with the stem changed."""
150148 suffix = self .suffix
151149 if not suffix :
@@ -156,7 +154,7 @@ def with_stem(self: _JP, stem: str) -> _JP:
156154 else :
157155 return self .with_name (stem + suffix )
158156
159- def with_suffix (self : _JP , suffix : str ) -> _JP :
157+ def with_suffix (self , suffix : str ) -> Self :
160158 """Return a new path with the file suffix changed. If the path
161159 has no suffix, add given suffix. If the given suffix is an empty
162160 string, remove the suffix from the path.
@@ -179,28 +177,28 @@ def parts(self) -> Sequence[str]:
179177 parts .append (anchor )
180178 return tuple (reversed (parts ))
181179
182- def joinpath (self : _JP , * pathsegments : str ) -> _JP :
180+ def joinpath (self , * pathsegments : str ) -> Self :
183181 """Combine this path with one or several arguments, and return a
184182 new path representing either a subpath (if all arguments are relative
185183 paths) or a totally different path (if one of the arguments is
186184 anchored).
187185 """
188186 return self .with_segments (str (self ), * pathsegments )
189187
190- def __truediv__ (self : _JP , key : str ) -> _JP :
188+ def __truediv__ (self , key : str ) -> Self :
191189 try :
192190 return self .with_segments (str (self ), key )
193191 except TypeError :
194192 return NotImplemented
195193
196- def __rtruediv__ (self : _JP , key : str ) -> _JP :
194+ def __rtruediv__ (self , key : str ) -> Self :
197195 try :
198196 return self .with_segments (key , str (self ))
199197 except TypeError :
200198 return NotImplemented
201199
202200 @property
203- def parent (self : _JP ) -> _JP :
201+ def parent (self ) -> Self :
204202 """The logical parent of the path."""
205203 path = str (self )
206204 parent = self .parser .split (path )[0 ]
@@ -209,7 +207,7 @@ def parent(self: _JP) -> _JP:
209207 return self
210208
211209 @property
212- def parents (self : _JP ) -> Sequence [_JP ]:
210+ def parents (self ) -> Sequence [Self ]:
213211 """A sequence of this path's logical parents."""
214212 split = self .parser .split
215213 path = str (self )
@@ -221,7 +219,7 @@ def parents(self: _JP) -> Sequence[_JP]:
221219 parent = split (path )[0 ]
222220 return tuple (parents )
223221
224- def full_match (self : _JP , pattern : str ) -> bool :
222+ def full_match (self , pattern : str ) -> bool :
225223 """
226224 Return True if this path matches the given glob-style pattern. The
227225 pattern is matched against the entire path.
@@ -278,15 +276,15 @@ def read_text(
278276 return f .read ()
279277
280278 @abstractmethod
281- def iterdir (self : _RP ) -> Iterator [_RP ]:
279+ def iterdir (self ) -> Iterator [Self ]:
282280 """Yield path objects of the directory contents.
283281
284282 The children are yielded in arbitrary order, and the
285283 special entries '.' and '..' are not included.
286284 """
287285 raise NotImplementedError
288286
289- def glob (self : _RP , pattern : str , * , recurse_symlinks : Literal [True ] = True ) -> Iterator [_RP ]:
287+ def glob (self , pattern : str , * , recurse_symlinks : Literal [True ] = True ) -> Iterator [Self ]:
290288 """Iterate over this subtree and yield all existing files (of any
291289 kind, including directories) matching the given relative pattern.
292290 """
@@ -303,11 +301,11 @@ def glob(self: _RP, pattern: str, *, recurse_symlinks: Literal[True] = True) ->
303301 return select (self .joinpath ('' ))
304302
305303 def walk (
306- self : _RP ,
304+ self ,
307305 top_down : bool = True ,
308306 on_error : Optional [Callable [[Exception ], None ]] = None ,
309307 follow_symlinks : bool = False ,
310- ) -> Generator [tuple [_RP , list [str ], list [str ]], None , None ]:
308+ ) -> Generator [tuple [Self , list [str ], list [str ]], None , None ]:
311309 """Walk the directory tree from this directory, similar to os.walk()."""
312310 paths = [self ]
313311 while paths :
@@ -339,7 +337,7 @@ def walk(
339337 paths += [path .joinpath (d ) for d in reversed (dirnames )]
340338
341339 @abstractmethod
342- def readlink (self : _RP ) -> _RP :
340+ def readlink (self ) -> Self :
343341 """
344342 Return the path to which the symbolic link points.
345343 """
0 commit comments