Skip to content

Commit caec352

Browse files
ClementSicardThe etils Authors
authored and
The etils Authors
committed
Bug fix for method .relative_to of etils.epath.Path
PiperOrigin-RevId: 738320692
1 parent fba407a commit caec352

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

etils/epath/gpath.py

+5
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ def open( # pytype: disable=signature-mismatch # overriding-parameter-count-ch
258258
gfile = typing.cast(typing.IO[Union[str, bytes]], gfile)
259259
return gfile
260260

261+
def relative_to(self: _P, other: PathLike) -> _P:
262+
"""Returns the current path relative to `other`."""
263+
other_path = self._new(other)
264+
return super().relative_to(other_path)
265+
261266
def rename(self: _P, target: PathLike) -> _P:
262267
"""Rename file or directory to the given target."""
263268
# Note: Issue if WindowsPath and target is gs://. Rather than using `_new`,

etils/epath/gpath_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,11 @@ def test_public_access():
455455
# Test a public bucket
456456
p = epath.Path('gs://tfds-data/datasets')
457457
assert p.exists()
458+
459+
460+
def test_relative_to():
461+
path = epath.Path('gs://bucket/dir/subdir')
462+
assert path.relative_to(path) == epath.Path('.')
463+
assert path.relative_to('gs://bucket/dir') == epath.Path('subdir')
464+
with pytest.raises(ValueError, match='not in the subpath'):
465+
path.relative_to('gs://bucket/other-dir')

0 commit comments

Comments
 (0)