no-unsafe-tempfile-factories

Require tempfile context managers instead of unmanaged mkstemp or mkdtemp calls.

Message

Use tempfile context managers instead of mkstemp or mkdtemp.

Settings

SettingDescriptionTypeDefault
excluded_path_parts list ['tests', 'benchmarks']

Valid examples

import tempfile

with tempfile.TemporaryDirectory() as path:
    use(path)
Show more
from tempfile import NamedTemporaryFile

with NamedTemporaryFile() as file:
    use(file.name)
from tempfile import mkstemp

factory().mkstemp()
class tempfile:
    @staticmethod
    def mkstemp():
        pass

tempfile.mkstemp()
import tempfile

tempfile = fake
tempfile.mkstemp()
import tempfile

def write_temp(tempfile):
    tempfile.mkstemp()
from tempfile import mkstemp

def write_temp(mkstemp):
    mkstemp()
from tempfile import *

def write_temp(mkstemp):
    mkstemp()
import tempfile

make_temp = tempfile.mkstemp

def write_temp(make_temp):
    make_temp()
import tempfile

make_temp = tempfile.mkstemp

def make_temp():
    pass

make_temp()

Invalid examples

import tempfile

fd, path = tempfile.mkstemp()
from tempfile import mkdtemp as make_temp_dir

path = make_temp_dir()
Show more
from tempfile import *

fd, path = mkstemp()
from tempfile import mkstemp

def write_temp():
    mkstemp = factory()
    mkstemp()

fd, path = mkstemp()
import tempfile

make_temp = tempfile.mkstemp
fd, path = make_temp()
from tempfile import mkdtemp

make_temp_dir = mkdtemp
path = make_temp_dir()
from tempfile import mkdtemp

make_temp_dir = other_make_temp_dir = mkdtemp
path = other_make_temp_dir()