Skip to content

Commit be2d2eb

Browse files
committed
GH-126381: pathlib ABCs: remove uncommon PurePathBase methods
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 12b4f1a commit be2d2eb

File tree

5 files changed

+365
-429
lines changed

5 files changed

+365
-429
lines changed

Lib/pathlib/_abc.py

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,6 @@ def __str__(self):
9797
paths.append('')
9898
return ''
9999

100-
def as_posix(self):
101-
"""Return the string representation of the path with forward (/)
102-
slashes."""
103-
return str(self).replace(self.parser.sep, '/')
104-
105-
@property
106-
def drive(self):
107-
"""The drive prefix (letter or UNC path), if any."""
108-
return self.parser.splitdrive(self.anchor)[0]
109-
110-
@property
111-
def root(self):
112-
"""The root of the path, if any."""
113-
return self.parser.splitdrive(self.anchor)[1]
114-
115100
@property
116101
def anchor(self):
117102
"""The concatenation of the drive and root, or ''."""
@@ -183,51 +168,6 @@ def with_suffix(self, suffix):
183168
else:
184169
return self.with_name(stem + suffix)
185170

186-
def relative_to(self, other, *, walk_up=False):
187-
"""Return the relative path to another path identified by the passed
188-
arguments. If the operation is not possible (because this is not
189-
related to the other path), raise ValueError.
190-
191-
The *walk_up* parameter controls whether `..` may be used to resolve
192-
the path.
193-
"""
194-
if not isinstance(other, PurePathBase):
195-
other = self.with_segments(other)
196-
anchor0, parts0 = self._stack
197-
anchor1, parts1 = other._stack
198-
if anchor0 != anchor1:
199-
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
200-
while parts0 and parts1 and parts0[-1] == parts1[-1]:
201-
parts0.pop()
202-
parts1.pop()
203-
for part in parts1:
204-
if not part or part == '.':
205-
pass
206-
elif not walk_up:
207-
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
208-
elif part == '..':
209-
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
210-
else:
211-
parts0.append('..')
212-
return self.with_segments(*reversed(parts0))
213-
214-
def is_relative_to(self, other):
215-
"""Return True if the path is relative to another path or False.
216-
"""
217-
if not isinstance(other, PurePathBase):
218-
other = self.with_segments(other)
219-
anchor0, parts0 = self._stack
220-
anchor1, parts1 = other._stack
221-
if anchor0 != anchor1:
222-
return False
223-
while parts0 and parts1 and parts0[-1] == parts1[-1]:
224-
parts0.pop()
225-
parts1.pop()
226-
for part in parts1:
227-
if part and part != '.':
228-
return False
229-
return True
230-
231171
@property
232172
def parts(self):
233173
"""An object providing sequence-like access to the
@@ -296,11 +236,6 @@ def parents(self):
296236
parent = split(path)[0]
297237
return tuple(parents)
298238

299-
def is_absolute(self):
300-
"""True if the path is absolute (has both a root and, if applicable,
301-
a drive)."""
302-
return self.parser.isabs(str(self))
303-
304239
@property
305240
def _pattern_str(self):
306241
"""The path expressed as a string, for use in pattern-matching."""

Lib/pathlib/_local.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ def _parse_pattern(cls, pattern):
297297
parts.append('')
298298
return parts
299299

300+
def as_posix(self):
301+
"""Return the string representation of the path with forward (/)
302+
slashes."""
303+
return str(self).replace(self.parser.sep, '/')
304+
300305
@property
301306
def drive(self):
302307
"""The drive prefix (letter or UNC path), if any."""

Lib/pathlib/_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ class Parser(Protocol):
1616
sep: str
1717
def join(self, path: str, *paths: str) -> str: ...
1818
def split(self, path: str) -> tuple[str, str]: ...
19-
def splitdrive(self, path: str) -> tuple[str, str]: ...
2019
def splitext(self, path: str) -> tuple[str, str]: ...
2120
def normcase(self, path: str) -> str: ...
22-
def isabs(self, path: str) -> bool: ...

0 commit comments

Comments
 (0)