diff --git a/upath/core.py b/upath/core.py index 71a07743..691c6675 100644 --- a/upath/core.py +++ b/upath/core.py @@ -234,9 +234,8 @@ def path(self) -> str: path = str(current_dir) else: path = current_dir.parser.join(str(self), self_path) - else: - path = str(self) - return self.parser.strip_protocol(path) + return self.parser.strip_protocol(path) + return self._chain.active_path def joinuri(self, uri: JoinablePathLike) -> UPath: """Join with urljoin behavior for UPath instances""" diff --git a/upath/implementations/cloud.py b/upath/implementations/cloud.py index 27bef5de..5c39108d 100644 --- a/upath/implementations/cloud.py +++ b/upath/implementations/cloud.py @@ -58,6 +58,13 @@ def __vfspath__(self) -> str: return f"{path}{self.root}" return path + @property + def path(self) -> str: + self_path = super().path + if self._relative_base is None and self.parser.sep not in self_path: + return self_path + self.root + return self_path + def mkdir( self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False ) -> None: diff --git a/upath/implementations/http.py b/upath/implementations/http.py index 4e4f4dbd..b2c2ee58 100644 --- a/upath/implementations/http.py +++ b/upath/implementations/http.py @@ -41,6 +41,11 @@ def __str__(self) -> str: sr = urlsplit(super().__str__()) return sr._replace(path=sr.path or "/").geturl() + @property + def path(self) -> str: + sr = urlsplit(super().path) + return sr._replace(path=sr.path or "/").geturl() + def is_file(self) -> bool: try: next(super().iterdir()) diff --git a/upath/tests/test_chain.py b/upath/tests/test_chain.py index dff872f8..8455dbcd 100644 --- a/upath/tests/test_chain.py +++ b/upath/tests/test_chain.py @@ -1,3 +1,4 @@ +import os from pathlib import Path import pytest @@ -18,6 +19,33 @@ def test_chaining_upath_protocol(urlpath, expected): assert pth.protocol == expected +def add_current_drive_on_windows(pth: str) -> str: + drive = os.path.splitdrive(Path.cwd().as_posix())[0] + return f"{drive}{pth}" + + +@pytest.mark.parametrize( + "urlpath,expected", + [ + pytest.param( + "simplecache::file:///tmp", + add_current_drive_on_windows("/tmp"), + ), + pytest.param( + "zip://file.txt::file:///tmp.zip", + "file.txt", + ), + pytest.param( + "zip://a/b/c.txt::simplecache::memory://zipfile.zip", + "a/b/c.txt", + ), + ], +) +def test_chaining_upath_path(urlpath, expected): + pth = UPath(urlpath) + assert pth.path == expected + + @pytest.mark.parametrize( "urlpath,expected", [