Skip to content

Commit ef63cca

Browse files
authored
pythonGH-127381: pathlib ABCs: remove uncommon PurePathBase methods (python#127853)
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.
1 parent c78729f commit ef63cca

File tree

5 files changed

+365
-433
lines changed

5 files changed

+365
-433
lines changed

Lib/pathlib/_abc.py

-65
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,6 @@ def __str__(self):
205205
passing to system calls."""
206206
raise NotImplementedError
207207

208-
def as_posix(self):
209-
"""Return the string representation of the path with forward (/)
210-
slashes."""
211-
return str(self).replace(self.parser.sep, '/')
212-
213-
@property
214-
def drive(self):
215-
"""The drive prefix (letter or UNC path), if any."""
216-
return self.parser.splitdrive(self.anchor)[0]
217-
218-
@property
219-
def root(self):
220-
"""The root of the path, if any."""
221-
return self.parser.splitdrive(self.anchor)[1]
222-
223208
@property
224209
def anchor(self):
225210
"""The concatenation of the drive and root, or ''."""
@@ -291,51 +276,6 @@ def with_suffix(self, suffix):
291276
else:
292277
return self.with_name(stem + suffix)
293278

294-
def relative_to(self, other, *, walk_up=False):
295-
"""Return the relative path to another path identified by the passed
296-
arguments. If the operation is not possible (because this is not
297-
related to the other path), raise ValueError.
298-
299-
The *walk_up* parameter controls whether `..` may be used to resolve
300-
the path.
301-
"""
302-
if not isinstance(other, PurePathBase):
303-
other = self.with_segments(other)
304-
anchor0, parts0 = _explode_path(self)
305-
anchor1, parts1 = _explode_path(other)
306-
if anchor0 != anchor1:
307-
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
308-
while parts0 and parts1 and parts0[-1] == parts1[-1]:
309-
parts0.pop()
310-
parts1.pop()
311-
for part in parts1:
312-
if not part or part == '.':
313-
pass
314-
elif not walk_up:
315-
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
316-
elif part == '..':
317-
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
318-
else:
319-
parts0.append('..')
320-
return self.with_segments(*reversed(parts0))
321-
322-
def is_relative_to(self, other):
323-
"""Return True if the path is relative to another path or False.
324-
"""
325-
if not isinstance(other, PurePathBase):
326-
other = self.with_segments(other)
327-
anchor0, parts0 = _explode_path(self)
328-
anchor1, parts1 = _explode_path(other)
329-
if anchor0 != anchor1:
330-
return False
331-
while parts0 and parts1 and parts0[-1] == parts1[-1]:
332-
parts0.pop()
333-
parts1.pop()
334-
for part in parts1:
335-
if part and part != '.':
336-
return False
337-
return True
338-
339279
@property
340280
def parts(self):
341281
"""An object providing sequence-like access to the
@@ -387,11 +327,6 @@ def parents(self):
387327
parent = split(path)[0]
388328
return tuple(parents)
389329

390-
def is_absolute(self):
391-
"""True if the path is absolute (has both a root and, if applicable,
392-
a drive)."""
393-
return self.parser.isabs(str(self))
394-
395330
def match(self, path_pattern, *, case_sensitive=None):
396331
"""
397332
Return True if this path matches the given pattern. If the pattern is

Lib/pathlib/_local.py

+5
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ def _parse_pattern(cls, pattern):
437437
parts.append('')
438438
return parts
439439

440+
def as_posix(self):
441+
"""Return the string representation of the path with forward (/)
442+
slashes."""
443+
return str(self).replace(self.parser.sep, '/')
444+
440445
@property
441446
def _raw_path(self):
442447
paths = self._raw_paths

Lib/pathlib/_types.py

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@ class Parser(Protocol):
1515

1616
sep: str
1717
def split(self, path: str) -> tuple[str, str]: ...
18-
def splitdrive(self, path: str) -> tuple[str, str]: ...
1918
def splitext(self, path: str) -> tuple[str, str]: ...
2019
def normcase(self, path: str) -> str: ...
21-
def isabs(self, path: str) -> bool: ...

0 commit comments

Comments
 (0)