Skip to content

[aiofiles] Fix NamedTemporaryFile name type - #16357

Open
nightcityblade wants to merge 2 commits into
python:mainfrom
nightcityblade:fix/issue-13551
Open

[aiofiles] Fix NamedTemporaryFile name type#16357
nightcityblade wants to merge 2 commits into
python:mainfrom
nightcityblade:fix/issue-13551

Conversation

@nightcityblade

Copy link
Copy Markdown
Contributor

Fixes #13551. Replaces #16162 after its branch history was repaired.

Models the path-dependent name attribute on wrappers returned by aiofiles.tempfile.NamedTemporaryFile(), preserving bytes when bytes path arguments are supplied while retaining str for string paths.

OpenAI Codex was used to prepare this change.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@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)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[aiofiles] aiofiles.tempfile.NamedTemporaryFile.name should probably be a str

2 participants