Skip to content

Commit

Permalink
GH-127381: pathlib ABCs: remove uncommon PurePathBase methods (#127853
Browse files Browse the repository at this point in the history
)

Remove `PurePathBase.relative_to()` and `is_relative_to()` because they
don't account for *other* being an entirely different kind of path, and
they can't use `__eq__()` because it's not on the `PurePathBase` interface.

Remove `PurePathBase.drive`, `root`, `is_absolute()` and `as_posix()`.
These are all too specific to local filesystems.
  • Loading branch information
barneygale authored Dec 29, 2024
1 parent c78729f commit ef63cca
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 433 deletions.
65 changes: 0 additions & 65 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,6 @@ def __str__(self):
passing to system calls."""
raise NotImplementedError

def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
return str(self).replace(self.parser.sep, '/')

@property
def drive(self):
"""The drive prefix (letter or UNC path), if any."""
return self.parser.splitdrive(self.anchor)[0]

@property
def root(self):
"""The root of the path, if any."""
return self.parser.splitdrive(self.anchor)[1]

@property
def anchor(self):
"""The concatenation of the drive and root, or ''."""
Expand Down Expand Up @@ -291,51 +276,6 @@ def with_suffix(self, suffix):
else:
return self.with_name(stem + suffix)

def relative_to(self, other, *, walk_up=False):
"""Return the relative path to another path identified by the passed
arguments. If the operation is not possible (because this is not
related to the other path), raise ValueError.
The *walk_up* parameter controls whether `..` may be used to resolve
the path.
"""
if not isinstance(other, PurePathBase):
other = self.with_segments(other)
anchor0, parts0 = _explode_path(self)
anchor1, parts1 = _explode_path(other)
if anchor0 != anchor1:
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
while parts0 and parts1 and parts0[-1] == parts1[-1]:
parts0.pop()
parts1.pop()
for part in parts1:
if not part or part == '.':
pass
elif not walk_up:
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
elif part == '..':
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
else:
parts0.append('..')
return self.with_segments(*reversed(parts0))

def is_relative_to(self, other):
"""Return True if the path is relative to another path or False.
"""
if not isinstance(other, PurePathBase):
other = self.with_segments(other)
anchor0, parts0 = _explode_path(self)
anchor1, parts1 = _explode_path(other)
if anchor0 != anchor1:
return False
while parts0 and parts1 and parts0[-1] == parts1[-1]:
parts0.pop()
parts1.pop()
for part in parts1:
if part and part != '.':
return False
return True

@property
def parts(self):
"""An object providing sequence-like access to the
Expand Down Expand Up @@ -387,11 +327,6 @@ def parents(self):
parent = split(path)[0]
return tuple(parents)

def is_absolute(self):
"""True if the path is absolute (has both a root and, if applicable,
a drive)."""
return self.parser.isabs(str(self))

def match(self, path_pattern, *, case_sensitive=None):
"""
Return True if this path matches the given pattern. If the pattern is
Expand Down
5 changes: 5 additions & 0 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ def _parse_pattern(cls, pattern):
parts.append('')
return parts

def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
return str(self).replace(self.parser.sep, '/')

@property
def _raw_path(self):
paths = self._raw_paths
Expand Down
2 changes: 0 additions & 2 deletions Lib/pathlib/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ class Parser(Protocol):

sep: str
def split(self, path: str) -> tuple[str, str]: ...
def splitdrive(self, path: str) -> tuple[str, str]: ...
def splitext(self, path: str) -> tuple[str, str]: ...
def normcase(self, path: str) -> str: ...
def isabs(self, path: str) -> bool: ...
Loading

0 comments on commit ef63cca

Please sign in to comment.