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
17 changes: 16 additions & 1 deletion upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from collections.abc import Mapping
from collections.abc import Sequence
from copy import copy
from fnmatch import fnmatchcase
from pathlib import PurePath
from types import MappingProxyType
from typing import IO
Expand Down Expand Up @@ -2208,7 +2209,21 @@ def match(
UserWarning,
stacklevel=2,
)
return self.full_match(path_pattern.replace("**", "*"))
path_pattern = path_pattern.replace("**", "*")
if self.full_match(path_pattern):
return True
# Like pathlib, a relative pattern is matched from the right, so
# UPath("s3://bucket/a/b").match("b") is True.
if path_pattern.startswith("/") or "://" in path_pattern:
return False
pattern_parts = path_pattern.rstrip("/").split("/")
path_parts = self.parts[1:] if self.anchor else self.parts
if len(pattern_parts) > len(path_parts):
return False
return all(
fnmatchcase(part, pat)
for part, pat in zip(reversed(path_parts), reversed(pattern_parts))
)

@classmethod
def __get_pydantic_core_schema__(
Expand Down
10 changes: 10 additions & 0 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ def test_full_match(self):
assert p.full_match("**/*.txt") is True
assert p.full_match("*.doesnotexist") is False

def test_match(self):
p = self.path / "folder" / "file.txt"
# relative patterns match from the right, like pathlib
assert p.match("file.txt") is True
assert p.match("*.txt") is True
assert p.match("folder/*.txt") is True
assert p.match("*/file.txt") is True
assert p.match("other/*.txt") is False
assert p.match("*.doesnotexist") is False

def test_trailing_slash_joinpath_is_identical(self):
# setup
cls = type(self.path)
Expand Down
5 changes: 5 additions & 0 deletions upath/tests/implementations/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def test_full_match(self):
assert self.path.full_match("*")
assert not self.path.full_match("xxx")

@overrides_base
def test_match(self):
assert self.path.match("*")
assert not self.path.match("xxx")

@overrides_base
def test_trailing_slash_joinpath_is_identical(self):
# DataPath has no slashes, and is not joinable
Expand Down