Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3573.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`trio.Path.samefile` no longer raises :exc:`AttributeError` when the path it is compared against is itself a :class:`trio.Path`.
11 changes: 10 additions & 1 deletion src/trio/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ async def open( # type: ignore[explicit-any] # Any usage matches builtins.open
def open(self, *args: Any, **kwargs: Any) -> AsyncIOWrapper[IO[Any]]: # type: ignore[misc, explicit-any] # Overload return mismatch.
return wrap_file(self._wrapped_cls(self).open(*args, **kwargs))

@_wraps_async(pathlib.Path.samefile)
def samefile(self, other_path: str | os.PathLike[str]) -> bool:
# pathlib asks ``other_path`` for its stat result and hands that
# straight to ``samestat``. Our ``stat`` is async, so when the other
# path is itself a `trio.Path` that would hand over an un-awaited
# coroutine. Comparing via ``os.path`` uses ``__fspath__`` on both
# sides instead, so any `os.PathLike` -- including a `trio.Path` --
# works.
return os.path.samefile(os.fspath(self), os.fspath(other_path))

def __repr__(self) -> str:
return f"trio.Path({str(self)!r})"

Expand Down Expand Up @@ -255,7 +265,6 @@ def __repr__(self) -> str:
expanduser = _wrap_method_path(pathlib.Path.expanduser)
read_bytes = _wrap_method(pathlib.Path.read_bytes)
read_text = _wrap_method(pathlib.Path.read_text)
samefile = _wrap_method(pathlib.Path.samefile)
write_bytes = _wrap_method(pathlib.Path.write_bytes)
write_text = _wrap_method(pathlib.Path.write_text)
if sys.version_info < (3, 12):
Expand Down
36 changes: 36 additions & 0 deletions src/trio/_tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,42 @@ async def test_async_methods_rewrap(method_name: str) -> None:
assert str(result) == str(async_result)


@pytest.mark.parametrize("other_is_trio_path", [False, True])
async def test_samefile(other_is_trio_path: bool, tmp_path: pathlib.Path) -> None:
# `samefile` passes the *other* path to `samestat` without going through
# `__fspath__`, so a trio.Path argument used to hand over an un-awaited
# coroutine from our async `stat`.
this = tmp_path / "this"
this.write_text("contents")
hardlink = tmp_path / "hardlink"
os.link(this, hardlink)
different = tmp_path / "different"
different.write_text("contents")

def other(path: pathlib.Path) -> pathlib.Path | trio.Path:
return trio.Path(path) if other_is_trio_path else path

path = trio.Path(this)

assert await path.samefile(other(this))
assert await path.samefile(other(hardlink))
assert not await path.samefile(other(different))
# and the reverse direction, where `other_path` is the hardlink
assert await trio.Path(hardlink).samefile(other(this))


@pytest.mark.parametrize("as_str", [False, True])
async def test_samefile_mixed_pathlike(as_str: bool, tmp_path: pathlib.Path) -> None:
# str and non-Path os.PathLike objects keep working too
this = tmp_path / "this"
this.write_text("contents")

path = trio.Path(this)
argument: str | os.PathLike[str] = str(this) if as_str else pathlib.PurePath(this)

assert await path.samefile(argument)


def test_forward_methods_rewrap(path: trio.Path, tmp_path: pathlib.Path) -> None:
with_name = path.with_name("foo")
with_suffix = path.with_suffix(".py")
Expand Down
Loading