|
5 | 5 |
|
6 | 6 | import argparse
|
7 | 7 | import concurrent.futures
|
| 8 | +import functools |
8 | 9 | import os
|
9 | 10 | import subprocess
|
10 | 11 | import sys
|
11 | 12 | import tempfile
|
12 | 13 | import time
|
13 | 14 | from collections import defaultdict
|
| 15 | +from collections.abc import Generator |
14 | 16 | from dataclasses import dataclass
|
15 | 17 | from enum import Enum
|
16 | 18 | from itertools import product
|
|
44 | 46 | print_error("Cannot import mypy. Did you install it?")
|
45 | 47 | sys.exit(1)
|
46 | 48 |
|
| 49 | +# We need to work around a limitation of tempfile.NamedTemporaryFile on Windows |
| 50 | +# For details, see https://github.com/python/typeshed/pull/13620#discussion_r1990185997 |
| 51 | +# Python 3.12 added a workaround with `tempfile.NamedTemporaryFile("w+", delete_on_close=False)` |
| 52 | +if sys.platform != "win32": |
| 53 | + _named_temporary_file = functools.partial(tempfile.NamedTemporaryFile, "w+") |
| 54 | +else: |
| 55 | + from contextlib import contextmanager |
| 56 | + |
| 57 | + @contextmanager |
| 58 | + def _named_temporary_file() -> Generator[tempfile._TemporaryFileWrapper[str]]: # pyright: ignore[reportPrivateUsage] |
| 59 | + temp = tempfile.NamedTemporaryFile("w+", delete=False) # noqa: SIM115 |
| 60 | + try: |
| 61 | + yield temp |
| 62 | + finally: |
| 63 | + temp.close() |
| 64 | + os.remove(temp.name) |
| 65 | + |
| 66 | + |
47 | 67 | SUPPORTED_VERSIONS = ["3.13", "3.12", "3.11", "3.10", "3.9"]
|
48 | 68 | SUPPORTED_PLATFORMS = ("linux", "win32", "darwin")
|
49 | 69 | DIRECTORIES_TO_TEST = [STDLIB_PATH, STUBS_PATH]
|
@@ -214,7 +234,8 @@ def run_mypy(
|
214 | 234 | env_vars = dict(os.environ)
|
215 | 235 | if mypypath is not None:
|
216 | 236 | env_vars["MYPYPATH"] = mypypath
|
217 |
| - with tempfile.NamedTemporaryFile("w+") as temp: |
| 237 | + |
| 238 | + with _named_temporary_file() as temp: |
218 | 239 | temp.write("[mypy]\n")
|
219 | 240 | for dist_conf in configurations:
|
220 | 241 | temp.write(f"[mypy-{dist_conf.module_name}]\n")
|
@@ -290,7 +311,7 @@ def add_third_party_files(
|
290 | 311 | if name.startswith("."):
|
291 | 312 | continue
|
292 | 313 | add_files(files, (root / name), args)
|
293 |
| - add_configuration(configurations, distribution) |
| 314 | + add_configuration(configurations, distribution) |
294 | 315 |
|
295 | 316 |
|
296 | 317 | class TestResult(NamedTuple):
|
|
0 commit comments