diff --git a/upath/core.py b/upath/core.py index 7937f3b7..d5260ba7 100644 --- a/upath/core.py +++ b/upath/core.py @@ -2001,8 +2001,13 @@ def resolve(self, strict: bool = False) -> Self: if ".." not in _parts and "." not in _parts: return self + # some flavours (github, webdav) don't put an anchor in .parts + if _parts[0].endswith(self.parser.sep): + anchor, resolvable_parts = _parts[0], _parts[1:] + else: + anchor, resolvable_parts = self.anchor, _parts + resolved: list[str] = [] - resolvable_parts = _parts[1:] for part in resolvable_parts: if part == "..": if resolved: @@ -2010,7 +2015,7 @@ def resolve(self, strict: bool = False) -> Self: elif part != ".": resolved.append(part) - return self.with_segments(*_parts[:1], *resolved) + return self.with_segments(anchor, *resolved) def touch(self, mode: int = 0o666, exist_ok: bool = True) -> None: """Create this file with the given access mode, if it doesn't exist.""" diff --git a/upath/tests/test_special_paths.py b/upath/tests/test_special_paths.py index 01b5d8b4..1b408e46 100644 --- a/upath/tests/test_special_paths.py +++ b/upath/tests/test_special_paths.py @@ -54,10 +54,6 @@ "candidate bug: relative paths given with protocol= are not rooted" " on sftp, smb and hdfs (not absolute, str() does not roundtrip)" ), - "resolve-no-anchor-part": ( - "candidate bug: resolve() skips the first part when .parts has no" - " anchor element (github, webdav) and leaves '..' in the result" - ), "parts-root-without-anchor": ( "candidate bug: .parts[0] is '/' although .anchor is '' (zip, tar, hf)" ), @@ -125,7 +121,6 @@ def _resolve(pth: UPath) -> UPath: NOT_ROOTED = dict.fromkeys(["sftp", "smb", "hdfs"], "relative-not-rooted") -NO_ANCHOR_PART = dict.fromkeys(["github", "webdav"], "resolve-no-anchor-part") # === (a) pathlib-backed local paths ================================== @@ -317,7 +312,7 @@ def test_single_dot_is_not_collapsed(protocol): assert p.name == "." -@pytest.mark.parametrize("protocol", _params(FSSPEC, NO_ANCHOR_PART)) +@pytest.mark.parametrize("protocol", _params(FSSPEC)) def test_resolve_normalizes_dot_segments(protocol): root = _root(protocol) for pth, expected in [ @@ -332,7 +327,7 @@ def test_resolve_normalizes_dot_segments(protocol): assert r == root / expected -@pytest.mark.parametrize("protocol", _params(FSSPEC, NO_ANCHOR_PART)) +@pytest.mark.parametrize("protocol", _params(FSSPEC)) def test_resolve_does_not_go_above_the_anchor(protocol): root = _root(protocol) assert _resolve(root / ".") == root @@ -343,7 +338,7 @@ def test_resolve_does_not_go_above_the_anchor(protocol): @pytest.mark.parametrize( "protocol", - _params(set(ROOTED) - CWD_RELATIVE, {**NOT_ROOTED, **NO_ANCHOR_PART}), + _params(set(ROOTED) - CWD_RELATIVE, NOT_ROOTED), ) def test_resolve_dot_is_root(protocol): assert _resolve(_kw(protocol, ".")) == _root(protocol)