Skip to content

Commit 21edea2

Browse files
authored
Fix relative_to for simplecache, smb, sftp and extensions (#458)
* tests: add test for relative_to * upath.core: fix relative_to * tests: skip data test case * upath.implementations.sftp: fix trailing slashes * upath.implementations.smb: fix trailing slashes * upath.extensions: fix relative_to for ProxyUPath * upath.core: fix error message use protocol
1 parent b6c3e58 commit 21edea2

6 files changed

Lines changed: 40 additions & 2 deletions

File tree

upath/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ def relative_to( # type: ignore[override]
13551355
if self.__class__ is not other.__class__:
13561356
raise ValueError(
13571357
"incompatible protocols:"
1358-
f" {self._protocol!r} != {other._protocol!r}"
1358+
f" {self.protocol!r} != {other.protocol!r}"
13591359
)
13601360
if self.storage_options != other.storage_options:
13611361
raise ValueError(
@@ -1371,7 +1371,7 @@ def relative_to( # type: ignore[override]
13711371
raise ValueError(f"{self!s} is not in the subpath of {other!s}")
13721372
else:
13731373
rel = copy(self)
1374-
rel._relative_base = str(other)
1374+
rel._relative_base = other.path
13751375
return rel
13761376

13771377
def is_relative_to(self, other, /, *_deprecated) -> bool: # type: ignore[override]

upath/extensions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ def relative_to( # type: ignore[override]
370370
*_deprecated,
371371
walk_up=False,
372372
) -> Self:
373+
if isinstance(other, ProxyUPath):
374+
other = other.__wrapped__
373375
return self._from_upath(
374376
self.__wrapped__.relative_to(other, *_deprecated, walk_up=walk_up)
375377
)

upath/implementations/sftp.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ def __init__(
3636
**storage_options: Unpack[SFTPStorageOptions],
3737
) -> None: ...
3838

39+
@property
40+
def path(self) -> str:
41+
path = super().path
42+
if len(path) > 1:
43+
return path.removesuffix("/")
44+
return path
45+
46+
def __str__(self) -> str:
47+
path_str = super().__str__()
48+
if path_str.startswith(("ssh:///", "sftp:///")):
49+
return path_str.removesuffix("/")
50+
return path_str
51+
3952
def iterdir(self) -> Iterator[Self]:
4053
if not self.is_dir():
4154
raise NotADirectoryError(str(self))

upath/implementations/smb.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ def __init__(
4040
**storage_options: Unpack[SMBStorageOptions],
4141
) -> None: ...
4242

43+
@property
44+
def path(self) -> str:
45+
path = super().path
46+
if len(path) > 1:
47+
return path.removesuffix("/")
48+
return path
49+
50+
def __str__(self) -> str:
51+
path_str = super().__str__()
52+
if path_str.startswith("smb:///"):
53+
return path_str.removesuffix("/")
54+
return path_str
55+
4356
def mkdir(
4457
self,
4558
mode: int = 0o777,

upath/tests/cases.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,9 @@ def test_move_into_memory(self, clear_fsspec_memory_cache):
655655
assert target.exists()
656656
assert target.read_text() == content
657657
assert not source.exists()
658+
659+
def test_relative_to(self):
660+
base = self.path
661+
child = self.path / "folder1" / "file1.txt"
662+
relative = child.relative_to(base)
663+
assert str(relative) == "folder1/file1.txt"

upath/tests/implementations/test_data.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,7 @@ def test_move_memory(self, clear_fsspec_memory_cache):
283283
@pytest.mark.skip(reason="DataPath does not support unlink")
284284
def test_move_into_memory(self, clear_fsspec_memory_cache):
285285
pass
286+
287+
@pytest.mark.skip(reason="DataPath does not support relative_to")
288+
def test_relative_to(self):
289+
pass

0 commit comments

Comments
 (0)