diff --git a/Lib/pathlib.py b/Lib/pathlib.py index e3eecc3b6d73e3..f7376577cdf3c3 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -647,9 +647,11 @@ def relative_to(self, other, /, *_deprecated, walk_up=False): "scheduled for removal in Python {remove}") warnings._deprecated("pathlib.PurePath.relative_to(*args)", msg, remove=(3, 14)) - other = self.with_segments(other, *_deprecated) + other = self.with_segments(other, *_deprecated) + elif not isinstance(other, PurePath): + other = self.with_segments(other) for step, path in enumerate([other] + list(other.parents)): - if self.is_relative_to(path): + if path == self or path in self.parents: break elif not walk_up: raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}") @@ -669,7 +671,9 @@ def is_relative_to(self, other, /, *_deprecated): "scheduled for removal in Python {remove}") warnings._deprecated("pathlib.PurePath.is_relative_to(*args)", msg, remove=(3, 14)) - other = self.with_segments(other, *_deprecated) + other = self.with_segments(other, *_deprecated) + elif not isinstance(other, PurePath): + other = self.with_segments(other) return other == self or other in self.parents @property diff --git a/Misc/NEWS.d/next/Library/2023-10-28-22-11-11.gh-issue-111429.mJGxuQ.rst b/Misc/NEWS.d/next/Library/2023-10-28-22-11-11.gh-issue-111429.mJGxuQ.rst new file mode 100644 index 00000000000000..c8bc4c5295a106 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-28-22-11-11.gh-issue-111429.mJGxuQ.rst @@ -0,0 +1,2 @@ +Speed up :meth:`pathlib.PurePath.relative_to` and +:meth:`~pathlib.PurePath.is_relative_to`.