Skip to content

Commit fab3a01

Browse files
committed
DirFS: Handle paths with no leading / (#1638)
1 parent 262f664 commit fab3a01

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

fsspec/implementations/dirfs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ 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 (self.path.startswith(self.fs.sep) and path == self.path[1:]):
6870
return ""
6971
prefix = self.path + self.fs.sep
72+
if self.path.startswith(self.fs.sep) and not path.startswith(self.fs.sep):
73+
prefix = prefix[1:]
7074
assert path.startswith(prefix)
7175
return path[len(prefix) :]
7276
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)