Skip to content
Open
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
9 changes: 7 additions & 2 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2001,16 +2001,21 @@ 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:
resolved.pop()
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."""
Expand Down
11 changes: 3 additions & 8 deletions upath/tests/test_special_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
),
Expand Down Expand Up @@ -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 ==================================
Expand Down Expand Up @@ -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 [
Expand All @@ -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
Expand All @@ -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)
Expand Down