Skip to content

Commit c457f06

Browse files
authored
Implement UPath.samefile (#261)
* tests: add samefile test case * upath: implement samefile * tests: datapath fix samefile test
1 parent bd5e5fa commit c457f06

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

upath/_stat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ def __repr__(self):
142142
seq_attrs = ", ".join(map("{0[0]}={0[1]}".format, zip(self._fields, self)))
143143
return f"{cls_name}({seq_attrs}, info={self._info!r})"
144144

145+
def __eq__(self, other):
146+
if not isinstance(other, UPathStatResult):
147+
return NotImplemented
148+
else:
149+
return self._info == other._info
150+
145151
# --- access to the fsspec info dict ------------------------------
146152

147153
@classmethod

upath/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,12 @@ def is_socket(self) -> bool:
803803
return False
804804

805805
def samefile(self, other_path) -> bool:
806-
raise NotImplementedError
806+
st = self.stat()
807+
if isinstance(other_path, UPath):
808+
other_st = other_path.stat()
809+
else:
810+
other_st = self.with_segments(other_path).stat()
811+
return st == other_st
807812

808813
@overload # type: ignore[override]
809814
def open(

upath/tests/cases.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,6 @@ def test_rglob(self, pathlib_base):
317317
expected = [*pathlib_base.rglob(pattern)]
318318
assert len(result) == len(expected)
319319

320-
def test_samefile(self):
321-
pass
322-
323320
def test_symlink_to(self):
324321
pass
325322

@@ -545,3 +542,12 @@ def test_eq(self):
545542
assert p0 == p1
546543
assert p0 != p2
547544
assert p1 != p2
545+
546+
def test_samefile(self):
547+
f1 = self.path.joinpath("file1.txt")
548+
f2 = self.path.joinpath("file2.txt")
549+
550+
assert f1.samefile(f2) is False
551+
assert f1.samefile(f2.path) is False
552+
assert f1.samefile(f1) is True
553+
assert f1.samefile(f1.path) is True

upath/tests/implementations/test_data.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,8 @@ def test_fsspec_compat(self):
199199
def test_rmdir_not_empty(self):
200200
with pytest.raises(NotADirectoryError):
201201
self.path.rmdir()
202+
203+
def test_samefile(self):
204+
f1 = self.path
205+
206+
assert f1.samefile(f1) is True

0 commit comments

Comments
 (0)