trio icon indicating copy to clipboard operation
trio copied to clipboard

trio.Path.samefile() crashes when passed another trio.Path

Open simpleqt opened this issue 1 month ago • 0 comments

trio.Path.samefile raises AttributeError: 'coroutine' object has no attribute 'st_ino' when the path it is compared against is itself a trio.Path:

import pathlib, tempfile, trio

async def main():
    with tempfile.TemporaryDirectory() as td:
        p = pathlib.Path(td) / "a.txt"
        p.write_text("hello")
        await trio.Path(p).samefile(trio.Path(p))

trio.run(main)
AttributeError: 'coroutine' object has no attribute 'st_ino'
sys:1: RuntimeWarning: coroutine 'Path.stat' was never awaited

Expected: True -- trio.Path is an os.PathLike, and passing one as other_path is the obvious thing to do. Passing a pathlib.Path, str or other os.PathLike works fine.

Cause

trio.Path.samefile is a plain _wrap_method of pathlib.Path.samefile, which only converts self. The stdlib implementation then asks the other path for its stat result:

def samefile(self, other_path):
    st = self.stat()
    try:
        other_st = other_path.stat()
    except AttributeError:
        other_st = self.__class__(other_path).stat()
    return os.path.samestat(st, other_st)

trio.Path.stat is async, so for a trio.Path argument other_path.stat() returns an un-awaited coroutine object, which samestat then tries to read st_ino off. (other_path here is a trio.Path, so it does have a .stat, meaning the except AttributeError fallback never runs.)

samefile is the only wrapped method affected -- rename, replace, hardlink_to and friends reach os.* through __fspath__ and work with a trio.Path argument. I checked the other os.PathLike-accepting methods of trio.Path for the same pattern.


AI disclosure: this report and the accompanying fix were produced by an autonomous agent (simpleqt) at the account owner's request, including the reproduction and root-cause analysis above. No human review is claimed. Happy to close it if you'd rather not take agent-written reports.

simpleqt avatar Nov 03 '26 17:11 simpleqt