GH-127807: pathlib ABCs: remove PurePathBase._raw_paths (#127883)

Remove the `PurePathBase` initializer, and make `with_segments()` and
`__str__()` abstract. This allows us to drop the `_raw_paths` attribute,
and also the `Parser.join()` protocol method.
This commit is contained in:
Barney Gale 2024-12-22 01:17:59 +00:00 committed by GitHub
parent 2a66dd33df
commit a959ea1b0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 92 additions and 96 deletions

View file

@ -44,49 +44,25 @@ class PurePathBase:
"""Base class for pure path objects.
This class *does not* provide several magic methods that are defined in
its subclass PurePath. They are: __fspath__, __bytes__, __reduce__,
__hash__, __eq__, __lt__, __le__, __gt__, __ge__. Its initializer and path
joining methods accept only strings, not os.PathLike objects more broadly.
its subclass PurePath. They are: __init__, __fspath__, __bytes__,
__reduce__, __hash__, __eq__, __lt__, __le__, __gt__, __ge__.
"""
__slots__ = (
# The `_raw_paths` slot stores unjoined string paths. This is set in
# the `__init__()` method.
'_raw_paths',
)
__slots__ = ()
parser = posixpath
_globber = PathGlobber
def __init__(self, *args):
for arg in args:
if not isinstance(arg, str):
raise TypeError(
f"argument should be a str, not {type(arg).__name__!r}")
self._raw_paths = list(args)
def with_segments(self, *pathsegments):
"""Construct a new path object from any number of path-like objects.
Subclasses may override this method to customize how new path objects
are created from methods like `iterdir()`.
"""
return type(self)(*pathsegments)
raise NotImplementedError
def __str__(self):
"""Return the string representation of the path, suitable for
passing to system calls."""
paths = self._raw_paths
if len(paths) == 1:
return paths[0]
elif paths:
# Join path segments from the initializer.
path = self.parser.join(*paths)
# Cache the joined path.
paths.clear()
paths.append(path)
return path
else:
paths.append('')
return ''
raise NotImplementedError
def as_posix(self):
"""Return the string representation of the path with forward (/)
@ -234,17 +210,17 @@ class PurePathBase:
paths) or a totally different path (if one of the arguments is
anchored).
"""
return self.with_segments(*self._raw_paths, *pathsegments)
return self.with_segments(str(self), *pathsegments)
def __truediv__(self, key):
try:
return self.with_segments(*self._raw_paths, key)
return self.with_segments(str(self), key)
except TypeError:
return NotImplemented
def __rtruediv__(self, key):
try:
return self.with_segments(key, *self._raw_paths)
return self.with_segments(key, str(self))
except TypeError:
return NotImplemented