Skip to content

Commit dbdae66

Browse files
authored
Fix chained paths access (#426)
* tests: add test to reproduce chained path access * upath: return correct .path for chained paths * upath.implementations: correct .path normalization for cloud and http paths * tests: add more chained tests * tests: add more chained tests * tests: fix test parameters for unix and win
1 parent fa3a086 commit dbdae66

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

upath/core.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,8 @@ def path(self) -> str:
234234
path = str(current_dir)
235235
else:
236236
path = current_dir.parser.join(str(self), self_path)
237-
else:
238-
path = str(self)
239-
return self.parser.strip_protocol(path)
237+
return self.parser.strip_protocol(path)
238+
return self._chain.active_path
240239

241240
def joinuri(self, uri: JoinablePathLike) -> UPath:
242241
"""Join with urljoin behavior for UPath instances"""

upath/implementations/cloud.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ def __vfspath__(self) -> str:
5858
return f"{path}{self.root}"
5959
return path
6060

61+
@property
62+
def path(self) -> str:
63+
self_path = super().path
64+
if self._relative_base is None and self.parser.sep not in self_path:
65+
return self_path + self.root
66+
return self_path
67+
6168
def mkdir(
6269
self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False
6370
) -> None:

upath/implementations/http.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def __str__(self) -> str:
4141
sr = urlsplit(super().__str__())
4242
return sr._replace(path=sr.path or "/").geturl()
4343

44+
@property
45+
def path(self) -> str:
46+
sr = urlsplit(super().path)
47+
return sr._replace(path=sr.path or "/").geturl()
48+
4449
def is_file(self) -> bool:
4550
try:
4651
next(super().iterdir())

upath/tests/test_chain.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path
23

34
import pytest
@@ -18,6 +19,33 @@ def test_chaining_upath_protocol(urlpath, expected):
1819
assert pth.protocol == expected
1920

2021

22+
def add_current_drive_on_windows(pth: str) -> str:
23+
drive = os.path.splitdrive(Path.cwd().as_posix())[0]
24+
return f"{drive}{pth}"
25+
26+
27+
@pytest.mark.parametrize(
28+
"urlpath,expected",
29+
[
30+
pytest.param(
31+
"simplecache::file:///tmp",
32+
add_current_drive_on_windows("/tmp"),
33+
),
34+
pytest.param(
35+
"zip://file.txt::file:///tmp.zip",
36+
"file.txt",
37+
),
38+
pytest.param(
39+
"zip://a/b/c.txt::simplecache::memory://zipfile.zip",
40+
"a/b/c.txt",
41+
),
42+
],
43+
)
44+
def test_chaining_upath_path(urlpath, expected):
45+
pth = UPath(urlpath)
46+
assert pth.path == expected
47+
48+
2149
@pytest.mark.parametrize(
2250
"urlpath,expected",
2351
[

0 commit comments

Comments
 (0)