Skip to content
Open
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
38 changes: 29 additions & 9 deletions stubs/aiofiles/aiofiles/tempfile/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,32 @@ from _typeshed import (
)
from asyncio import AbstractEventLoop
from concurrent.futures import Executor
from typing import AnyStr, Literal, overload
from typing import AnyStr, Generic, Literal, overload, type_check_only

from ..base import AiofilesContextManager
from ..threadpool.binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO
from ..threadpool.text import AsyncTextIOWrapper

@type_check_only
class _NamedAsyncBufferedIOBase(AsyncBufferedIOBase, Generic[AnyStr]):
@property
def name(self) -> AnyStr: ...

@type_check_only
class _NamedAsyncBufferedReader(AsyncBufferedReader, Generic[AnyStr]):
@property
def name(self) -> AnyStr: ...

@type_check_only
class _NamedAsyncFileIO(AsyncFileIO, Generic[AnyStr]):
@property
def name(self) -> AnyStr: ...

@type_check_only
class _NamedAsyncTextIOWrapper(AsyncTextIOWrapper, Generic[AnyStr]):
@property
def name(self) -> AnyStr: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering these all derive from _UnknownAsyncBinaryIO or _UnknownAsyncTextIO, I think a better approach is to make these classes and their sub-classes generic over name.

_NameT =  TypeVar("_NameT", bound=FileDescriptorOrPath, default=FileDescriptorOrPath)


# Text mode: always returns AsyncTextIOWrapper
@overload
def TemporaryFile(
Expand Down Expand Up @@ -89,7 +109,7 @@ if sys.version_info >= (3, 12):
delete_on_close: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncTextIOWrapper]: ...
) -> AiofilesContextManager[_NamedAsyncTextIOWrapper[AnyStr]]: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using AnyStr here or in the other branches is correct. For one, if neither suffix or prefix is given, AnyStr is unbound. Also the name attribute of the wrapped temporary file object always seems to to be str. This would require further investigation.


# Unbuffered binary: returns a FileIO
@overload
Expand All @@ -105,7 +125,7 @@ if sys.version_info >= (3, 12):
delete_on_close: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncFileIO]: ...
) -> AiofilesContextManager[_NamedAsyncFileIO[AnyStr]]: ...

# Buffered binary reading/updating: AsyncBufferedReader
@overload
Expand All @@ -121,7 +141,7 @@ if sys.version_info >= (3, 12):
delete_on_close: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncBufferedReader]: ...
) -> AiofilesContextManager[_NamedAsyncBufferedReader[AnyStr]]: ...

# Buffered binary writing: AsyncBufferedIOBase
@overload
Expand All @@ -137,7 +157,7 @@ if sys.version_info >= (3, 12):
delete_on_close: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncBufferedIOBase]: ...
) -> AiofilesContextManager[_NamedAsyncBufferedIOBase[AnyStr]]: ...
else:
# Text mode: always returns AsyncTextIOWrapper
@overload
Expand All @@ -152,7 +172,7 @@ else:
delete: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncTextIOWrapper]: ...
) -> AiofilesContextManager[_NamedAsyncTextIOWrapper[AnyStr]]: ...

# Unbuffered binary: returns a FileIO
@overload
Expand All @@ -167,7 +187,7 @@ else:
delete: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncFileIO]: ...
) -> AiofilesContextManager[_NamedAsyncFileIO[AnyStr]]: ...

# Buffered binary reading/updating: AsyncBufferedReader
@overload
Expand All @@ -182,7 +202,7 @@ else:
delete: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncBufferedReader]: ...
) -> AiofilesContextManager[_NamedAsyncBufferedReader[AnyStr]]: ...

# Buffered binary writing: AsyncBufferedIOBase
@overload
Expand All @@ -197,7 +217,7 @@ else:
delete: bool = True,
loop: AbstractEventLoop | None = None,
executor: Executor | None = None,
) -> AiofilesContextManager[AsyncBufferedIOBase]: ...
) -> AiofilesContextManager[_NamedAsyncBufferedIOBase[AnyStr]]: ...

# Text mode: always returns AsyncTextIOWrapper
@overload
Expand Down