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:

cpython/Lib/tempfile.py

Lines 620 to 623 in 0c29f83

if _os.name != 'posix' or _sys.platform == 'cygwin':
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
# while it is open.
TemporaryFile = NamedTemporaryFile

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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions