Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ def relative_to( # type: ignore[override]
if self.__class__ is not other.__class__:
raise ValueError(
"incompatible protocols:"
f" {self._protocol!r} != {other._protocol!r}"
f" {self.protocol!r} != {other.protocol!r}"
)
if self.storage_options != other.storage_options:
raise ValueError(
Expand All @@ -1371,7 +1371,7 @@ def relative_to( # type: ignore[override]
raise ValueError(f"{self!s} is not in the subpath of {other!s}")
else:
rel = copy(self)
rel._relative_base = str(other)
rel._relative_base = other.path
return rel

def is_relative_to(self, other, /, *_deprecated) -> bool: # type: ignore[override]
Expand Down
2 changes: 2 additions & 0 deletions upath/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ def relative_to( # type: ignore[override]
*_deprecated,
walk_up=False,
) -> Self:
if isinstance(other, ProxyUPath):
other = other.__wrapped__
return self._from_upath(
self.__wrapped__.relative_to(other, *_deprecated, walk_up=walk_up)
)
Expand Down
13 changes: 13 additions & 0 deletions upath/implementations/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def __init__(
**storage_options: Unpack[SFTPStorageOptions],
) -> None: ...

@property
def path(self) -> str:
path = super().path
if len(path) > 1:
return path.removesuffix("/")
return path

def __str__(self) -> str:
path_str = super().__str__()
if path_str.startswith(("ssh:///", "sftp:///")):
return path_str.removesuffix("/")
return path_str

def iterdir(self) -> Iterator[Self]:
if not self.is_dir():
raise NotADirectoryError(str(self))
Expand Down
13 changes: 13 additions & 0 deletions upath/implementations/smb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def __init__(
**storage_options: Unpack[SMBStorageOptions],
) -> None: ...

@property
def path(self) -> str:
path = super().path
if len(path) > 1:
return path.removesuffix("/")
return path

def __str__(self) -> str:
path_str = super().__str__()
if path_str.startswith("smb:///"):
return path_str.removesuffix("/")
return path_str

def mkdir(
self,
mode: int = 0o777,
Expand Down
6 changes: 6 additions & 0 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,9 @@ def test_move_into_memory(self, clear_fsspec_memory_cache):
assert target.exists()
assert target.read_text() == content
assert not source.exists()

def test_relative_to(self):
base = self.path
child = self.path / "folder1" / "file1.txt"
relative = child.relative_to(base)
assert str(relative) == "folder1/file1.txt"
4 changes: 4 additions & 0 deletions upath/tests/implementations/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,7 @@ def test_move_memory(self, clear_fsspec_memory_cache):
@pytest.mark.skip(reason="DataPath does not support unlink")
def test_move_into_memory(self, clear_fsspec_memory_cache):
pass

@pytest.mark.skip(reason="DataPath does not support relative_to")
def test_relative_to(self):
pass