Skip to content

TemporaryFile args not consistent across platforms #145504

@shaperilio

Description

@shaperilio

Bug report

Bug description:

In the tempfile module, TemporaryFile is defined as an alias for NamedTemporaryFile for non-posix systems:
https://github.com/python/cpython/blob/main/Lib/tempfile.py#L620-L623

That means that in, e.g. Linux, the args are:

def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
                      newline=None, suffix=None, prefix=None,
                      dir=None, *, errors=None)

while in, e.g. Windows, they are:

def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
                       newline=None, suffix=None, prefix=None,
                       dir=None, delete=True, *, errors=None,
                       delete_on_close=True)

Static checkers such as VSCode pick up on this and let me write code like this:

with TemporaryFile(delete=False) as tmp:
    ...

and it will run fine in Windows but not anywhere else.

I believe TemporaryFile should not just be a simple alias for non-posix systems; it can certainly wrap NamedTemporaryFile, but should have the exact same signature across platforms.

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions