[aiofiles] .name on NamedTempFile is always a str#13621
[aiofiles] .name on NamedTempFile is always a str#13621
.name on NamedTempFile is always a str#13621Conversation
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
| def newlines(self) -> str | tuple[str, ...] | None: ... | ||
| @property | ||
| def name(self) -> FileDescriptorOrPath: ... | ||
| def name(self) -> str: ... |
There was a problem hiding this comment.
I don't think we can do this unconditionally. aiofiles.threadpool.open() will also return an AsyncTextIOWrapper, which might be a file descriptor:
import asyncio
from os import fdopen
from aiofiles import open as aioopen
async def main():
f = open("bar.txt")
async with aioopen(f.fileno()) as aiof:
print(type(aiof.name)) # <class 'int'>
asyncio.run(main())Maybe it's best if we make _UnknownAsyncTextIO and the two wrapper classes generic over name with a default of FileDescriptorOrPath. Then we can bind this to str in the return type of NamedTemporaryFile.
In fact, we could use this as a template for the stdlib, where we already have a comment alluding to this:
Lines 771 to 774 in 0b8dcfc
There was a problem hiding this comment.
Great idea! Thanks a lot! ❤️
There was a problem hiding this comment.
Now, when I double checked that, I think that I would prefer to close this PR, because it is too complex. Notice that def NamedTemporaryFile has multiple @overloads that return different subtypes of _UnknownAsyncBinaryIO, not even direct ones. I think that making all of the generic is an overkill :(
There was a problem hiding this comment.
@sobolevn 1 year and 8 hours later - can this be closed?
Closes #13551
Basically, this is a wrapper around
typeshed/stdlib/tempfile.pyi
Line 219 in 28106bc