From 37fdc0f58494e046d7178de3d7a1af2476b6f58e Mon Sep 17 00:00:00 2001 From: itzzdev09 Date: Mon, 16 Nov 2026 18:52:14 +0530 Subject: [PATCH] Keep the trailing separator in glob and rglob patterns A pattern ending in a separator, such as "*/", matches only directories in pathlib and in fsspec's glob. UPath.glob() and rglob() built the fsspec pattern with joinpath(), which drops the trailing separator, so files were returned as well. --- upath/core.py | 15 +++++++++------ upath/tests/cases.py | 5 +++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/upath/core.py b/upath/core.py index f1903ae7..50311a9a 100644 --- a/upath/core.py +++ b/upath/core.py @@ -1728,8 +1728,10 @@ def glob( ) if self._relative_base is not None: self = self.absolute() - path_pattern = self.joinpath(pattern).path sep = self.parser.sep + # joinpath drops a trailing separator, which restricts matches to directories + dirs_only = sep if isinstance(pattern, str) and pattern.endswith(sep) else "" + path_pattern = self.joinpath(pattern).path + dirs_only base = self.path for name in self.fs.glob(path_pattern): name = name.removeprefix(base).removeprefix(sep) @@ -1761,18 +1763,19 @@ def rglob( if _FSSPEC_HAS_WORKING_GLOB is None: _check_fsspec_has_working_glob() + sep = self.parser.sep + # joinpath drops a trailing separator, which restricts matches to directories + dirs_only = sep if isinstance(pattern, str) and pattern.endswith(sep) else "" if _FSSPEC_HAS_WORKING_GLOB: - r_path_pattern = self.joinpath("**", pattern).path - sep = self.parser.sep + r_path_pattern = self.joinpath("**", pattern).path + dirs_only base = self.path for name in self.fs.glob(r_path_pattern): name = name.removeprefix(base).removeprefix(sep) yield self.joinpath(name) else: - path_pattern = self.joinpath(pattern).path - r_path_pattern = self.joinpath("**", pattern).path - sep = self.parser.sep + path_pattern = self.joinpath(pattern).path + dirs_only + r_path_pattern = self.joinpath("**", pattern).path + dirs_only base = self.path seen = set() for p in (path_pattern, r_path_pattern): diff --git a/upath/tests/cases.py b/upath/tests/cases.py index 47407bdc..6ab129d2 100644 --- a/upath/tests/cases.py +++ b/upath/tests/cases.py @@ -334,6 +334,7 @@ def test_expanduser(self): ( "*.txt", "*", + "*/", pytest.param( "**/*.txt", marks=( @@ -478,8 +479,8 @@ def test_read_text_errors(self): content = upath.read_text(encoding="ascii", errors="strict") assert content == "hello world" - def test_rglob(self, pathlib_base): - pattern = "*.txt" + @pytest.mark.parametrize("pattern", ["*.txt", "*/"]) + def test_rglob(self, pathlib_base, pattern): result = [*self.path.rglob(pattern)] expected = [*pathlib_base.rglob(pattern)] assert len(result) == len(expected)