Skip to content

Commit 0cec5bd

Browse files
authored
Merge pull request pre-commit#2664 from pre-commit/special-rmtree-no-longer-needed
special rmtree is not needed for TemporaryDirectory in 3.8+
2 parents 092e9a5 + 0a0754e commit 0cec5bd

File tree

4 files changed

+4
-24
lines changed

4 files changed

+4
-24
lines changed

pre_commit/commands/autoupdate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os.path
44
import re
5+
import tempfile
56
from typing import Any
67
from typing import NamedTuple
78
from typing import Sequence
@@ -19,7 +20,6 @@
1920
from pre_commit.util import CalledProcessError
2021
from pre_commit.util import cmd_output
2122
from pre_commit.util import cmd_output_b
22-
from pre_commit.util import tmpdir
2323
from pre_commit.util import yaml_dump
2424
from pre_commit.util import yaml_load
2525

@@ -47,7 +47,7 @@ def update(self, tags_only: bool, freeze: bool) -> RevInfo:
4747
'FETCH_HEAD', '--tags', '--exact',
4848
)
4949

50-
with tmpdir() as tmp:
50+
with tempfile.TemporaryDirectory() as tmp:
5151
git.init_repo(tmp, self.repo)
5252
cmd_output_b(
5353
*git_cmd, 'fetch', 'origin', 'HEAD', '--tags',

pre_commit/commands/try_repo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import logging
55
import os.path
6+
import tempfile
67

78
import pre_commit.constants as C
89
from pre_commit import git
@@ -11,7 +12,6 @@
1112
from pre_commit.commands.run import run
1213
from pre_commit.store import Store
1314
from pre_commit.util import cmd_output_b
14-
from pre_commit.util import tmpdir
1515
from pre_commit.util import yaml_dump
1616
from pre_commit.xargs import xargs
1717

@@ -49,7 +49,7 @@ def _repo_ref(tmpdir: str, repo: str, ref: str | None) -> tuple[str, str]:
4949

5050

5151
def try_repo(args: argparse.Namespace) -> int:
52-
with tmpdir() as tempdir:
52+
with tempfile.TemporaryDirectory() as tempdir:
5353
repo, ref = _repo_ref(tempdir, args.repo, args.ref)
5454

5555
store = Store(tempdir)

pre_commit/util.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import stat
1010
import subprocess
1111
import sys
12-
import tempfile
1312
from types import TracebackType
1413
from typing import Any
1514
from typing import Callable
@@ -52,18 +51,6 @@ def clean_path_on_failure(path: str) -> Generator[None, None, None]:
5251
raise
5352

5453

55-
@contextlib.contextmanager
56-
def tmpdir() -> Generator[str, None, None]:
57-
"""Contextmanager to create a temporary directory. It will be cleaned up
58-
afterwards.
59-
"""
60-
tempdir = tempfile.mkdtemp()
61-
try:
62-
yield tempdir
63-
finally:
64-
rmtree(tempdir)
65-
66-
6754
def resource_bytesio(filename: str) -> IO[bytes]:
6855
return importlib.resources.open_binary('pre_commit.resources', filename)
6956

tests/util_test.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pre_commit.util import make_executable
1515
from pre_commit.util import parse_version
1616
from pre_commit.util import rmtree
17-
from pre_commit.util import tmpdir
1817

1918

2019
def test_CalledProcessError_str():
@@ -74,12 +73,6 @@ class MySystemExit(SystemExit):
7473
assert not os.path.exists('foo')
7574

7675

77-
def test_tmpdir():
78-
with tmpdir() as tempdir:
79-
assert os.path.exists(tempdir)
80-
assert not os.path.exists(tempdir)
81-
82-
8376
def test_cmd_output_exe_not_found():
8477
ret, out, _ = cmd_output('dne', check=False)
8578
assert ret == 1

0 commit comments

Comments
 (0)