actually need to swap it out

This commit is contained in:
sneakers-the-rat 2024-09-23 16:24:35 -07:00
parent 3d326dfc8b
commit ea0e8127fa
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D

View file

@ -85,26 +85,26 @@ def _walk_and_apply(value: T, f: Callable[[U], U]) -> T:
return value return value
def relative_path(target: Path, origin: Path) -> Path: # def relative_path(target: Path, origin: Path) -> Path:
""" # """
return path of target relative to origin, even if they're # return path of target relative to origin, even if they're
not in the same subpath # not in the same subpath
#
References: # References:
- https://stackoverflow.com/a/71874881 # - https://stackoverflow.com/a/71874881
""" # """
try: # try:
return Path(target).resolve().relative_to(Path(origin).resolve()) # return Path(target).resolve().relative_to(Path(origin).resolve())
except ValueError: # target does not start with origin # except ValueError: # target does not start with origin
# recursion with origin (eventually origin is root so try will succeed) # # recursion with origin (eventually origin is root so try will succeed)
try: # try:
return Path("..").joinpath(relative_path(target, Path(origin).parent)) # return Path("..").joinpath(relative_path(target, Path(origin).parent))
except ValueError: # except ValueError:
# break recursion in windows when # # break recursion in windows when
pass # pass
def relative_to(self: Path, other: Path, walk_up=True) -> Path: def relative_path(self: Path, other: Path, walk_up: bool = True) -> Path:
""" """
"Backport" of :meth:`pathlib.Path.relative_to` with ``walk_up=True`` "Backport" of :meth:`pathlib.Path.relative_to` with ``walk_up=True``
that's not available pre 3.12. that's not available pre 3.12.
@ -115,6 +115,9 @@ def relative_to(self: Path, other: Path, walk_up=True) -> Path:
The *walk_up* parameter controls whether `..` may be used to resolve The *walk_up* parameter controls whether `..` may be used to resolve
the path. the path.
References:
https://github.com/python/cpython/blob/8a2baedc4bcb606da937e4e066b4b3a18961cace/Lib/pathlib/_abc.py#L244-L270
""" """
if not isinstance(other, Path): if not isinstance(other, Path):
other = self.with_segments(other) other = self.with_segments(other)