diff --git a/mypy/test/data.py b/mypy/test/data.py index ee567afe2125..bc17178d20e0 100644 --- a/mypy/test/data.py +++ b/mypy/test/data.py @@ -304,7 +304,7 @@ def __init__( self.data = data self.line = line self.old_cwd: str | None = None - self.tmpdir: tempfile.TemporaryDirectory[str] | None = None + self.tmpdir: str | None = None def runtest(self) -> None: if self.skip: @@ -323,19 +323,19 @@ def runtest(self) -> None: save_dir: str | None = self.config.getoption("--save-failures-to", None) if save_dir: assert self.tmpdir is not None - target_dir = os.path.join(save_dir, os.path.basename(self.tmpdir.name)) + target_dir = os.path.join(save_dir, os.path.basename(self.tmpdir)) print(f"Copying data from test {self.name} to {target_dir}") if not os.path.isabs(target_dir): assert self.old_cwd target_dir = os.path.join(self.old_cwd, target_dir) - shutil.copytree(self.tmpdir.name, target_dir) + shutil.copytree(self.tmpdir, target_dir) raise def setup(self) -> None: parse_test_case(case=self) self.old_cwd = os.getcwd() - self.tmpdir = tempfile.TemporaryDirectory(prefix="mypy-test-") - os.chdir(self.tmpdir.name) + self.tmpdir = tempfile.mkdtemp(prefix="mypy-test-") + os.chdir(self.tmpdir) os.mkdir(test_temp_dir) # Precalculate steps for find_steps() @@ -371,10 +371,7 @@ def teardown(self) -> None: if self.old_cwd is not None: os.chdir(self.old_cwd) if self.tmpdir is not None: - try: - self.tmpdir.cleanup() - except OSError: - pass + shutil.rmtree(self.tmpdir, ignore_errors=True) self.old_cwd = None self.tmpdir = None diff --git a/mypy/test/testfinegrained.py b/mypy/test/testfinegrained.py index 800ba2dff087..cb8672dfaf29 100644 --- a/mypy/test/testfinegrained.py +++ b/mypy/test/testfinegrained.py @@ -101,8 +101,8 @@ def run_case(self, testcase: DataDrivenTestCase) -> None: if messages: a.extend(normalize_messages(messages)) - assert testcase.tmpdir - a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir.name)) + assert testcase.tmpdir is not None + a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir)) a.extend(self.maybe_inspect(step, server, main_src)) if server.fine_grained_manager: @@ -248,8 +248,8 @@ def perform_step( new_messages = normalize_messages(new_messages) a = new_messages - assert testcase.tmpdir - a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir.name)) + assert testcase.tmpdir is not None + a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir)) a.extend(self.maybe_inspect(step, server, main_src)) return a, triggered