Skip to content

Commit 7fee7c1

Browse files
DirFS: Handle paths with no leading / (#1638) (#1639)
Co-authored-by: Martin Durant <[email protected]>
1 parent 262f664 commit 7fee7c1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

fsspec/implementations/dirfs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ def _relpath(self, path):
6464
if isinstance(path, str):
6565
if not self.path:
6666
return path
67-
if path == self.path:
67+
# We need to account for S3FileSystem returning paths that do not
68+
# start with a '/'
69+
if path == self.path or (
70+
self.path.startswith(self.fs.sep) and path == self.path[1:]
71+
):
6872
return ""
6973
prefix = self.path + self.fs.sep
74+
if self.path.startswith(self.fs.sep) and not path.startswith(self.fs.sep):
75+
prefix = prefix[1:]
7076
assert path.startswith(prefix)
7177
return path[len(prefix) :]
7278
return [self._relpath(_path) for _path in path]

fsspec/implementations/tests/test_dirfs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def test_dirfs(fs, asyncfs):
8282
("", "foo", "foo"),
8383
("root", "", "root"),
8484
("root", "foo", "root/foo"),
85+
("/root", "", "/root"),
86+
("/root", "foo", "/root/foo"),
8587
],
8688
)
8789
def test_path(fs, root, rel, full):
@@ -90,6 +92,18 @@ def test_path(fs, root, rel, full):
9092
assert dirfs._relpath(full) == rel
9193

9294

95+
@pytest.mark.parametrize(
96+
"root, rel, full",
97+
[
98+
("/root", "foo", "root/foo"),
99+
("/root", "", "root"),
100+
],
101+
)
102+
def test_path_no_leading_slash(fs, root, rel, full):
103+
dirfs = DirFileSystem(root, fs)
104+
assert dirfs._relpath(full) == rel
105+
106+
93107
def test_sep(mocker, dirfs):
94108
sep = mocker.Mock()
95109
dirfs.fs.sep = sep

0 commit comments

Comments
 (0)