Closed altendky closed 2 years ago
We also see mypy error for read()
:
async with await path.open('rb') as f:
buf = await f.read()
error: "_AsyncIOBase" has no attribute "read"
I think both of these would resolve if you use the plugin — in general the set of valid methods on a file object depend on the mode you used to open the file. But it would probably be sensible to update trio-typing to:
I’ll leave this issue open to track that.
Problem
You can't (per hints)
.write()
to a file you have opened. Though you can.writelines()
and.flush()
...Opening a
trio.Path
...https://github.com/python-trio/trio-typing/blob/f32f17b0f242daf2d42407f383ca581d64b6c299/trio-stubs/__init__.pyi#L397-L404
results in an
_AsyncIOBase
.https://github.com/python-trio/trio-typing/blob/f32f17b0f242daf2d42407f383ca581d64b6c299/trio-stubs/__init__.pyi#L309-L325
But,
.write()
is located on_AsyncRawIOBase
.https://github.com/python-trio/trio-typing/blob/f32f17b0f242daf2d42407f383ca581d64b6c299/trio-stubs/__init__.pyi#L327-L331
Cause
It appears that Trio actually returns an
AsyncIOWrapper
, not the hinted_AsyncIOBase
, and only after verifying that the object being wrapped does in fact have.write()
(and more).https://github.com/python-trio/trio/blob/master/trio/_path.py#L174-L183
https://github.com/python-trio/trio/blob/d203b30f807e43dc9b101de2e52355ebd10b757a/trio/_file_io.py#L164-L191
Also,
AsyncIOWrapper
does look to specifically list.write()
as a (trio.to_thread.run_sync
) pass-through method.https://github.com/python-trio/trio/blob/d203b30f807e43dc9b101de2e52355ebd10b757a/trio/_file_io.py#L70-L85
Solution
Per a GitHub search,
trio-typing
just doesn't haveAsyncIOWrapper
. I guess it should get added totrio-typing
and used as the return hint fortrio.Path.open()
. Let me know if there's agreement here, or if I have missed or am just unaware of something that makes this change wrong.