Move imports of ThreadPoolExecutor into reformat_many(), allowing Black-in-the-browser (#3046)

This is a slight perf win for use-cases that don't invoke `reformat_many()`, but more importantly to me today it means I can use Black in pyscript.
This commit is contained in:
Zac Hatfield-Dodds 2022-05-06 07:06:27 -07:00 committed by GitHub
parent c940f75d5b
commit 9ce100ba61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -1,7 +1,6 @@
import asyncio
from json.decoder import JSONDecodeError
import json
from concurrent.futures import Executor, ThreadPoolExecutor, ProcessPoolExecutor
from contextlib import contextmanager
from datetime import datetime
from enum import Enum
@ -17,6 +16,7 @@
import tokenize
import traceback
from typing import (
TYPE_CHECKING,
Any,
Dict,
Generator,
@ -77,6 +77,9 @@
from _black_version import version as __version__
if TYPE_CHECKING:
from concurrent.futures import Executor
COMPILED = Path(__file__).suffix in (".pyd", ".so")
# types
@ -767,6 +770,8 @@ def reformat_many(
workers: Optional[int],
) -> None:
"""Reformat multiple files using a ProcessPoolExecutor."""
from concurrent.futures import Executor, ThreadPoolExecutor, ProcessPoolExecutor
executor: Executor
loop = asyncio.get_event_loop()
worker_count = workers if workers is not None else DEFAULT_WORKERS
@ -808,7 +813,7 @@ async def schedule_formatting(
mode: Mode,
report: "Report",
loop: asyncio.AbstractEventLoop,
executor: Executor,
executor: "Executor",
) -> None:
"""Run formatting of `sources` in parallel using the provided `executor`.

View File

@ -922,7 +922,7 @@ def err(msg: str, **kwargs: Any) -> None:
self.assertEqual("".join(err_lines), "")
@event_loop()
@patch("black.ProcessPoolExecutor", MagicMock(side_effect=OSError))
@patch("concurrent.futures.ProcessPoolExecutor", MagicMock(side_effect=OSError))
def test_works_in_mono_process_only_environment(self) -> None:
with cache_dir() as workspace:
for f in [
@ -1683,7 +1683,7 @@ def test_cache_single_file_already_cached(self) -> None:
def test_cache_multiple_files(self) -> None:
mode = DEFAULT_MODE
with cache_dir() as workspace, patch(
"black.ProcessPoolExecutor", new=ThreadPoolExecutor
"concurrent.futures.ProcessPoolExecutor", new=ThreadPoolExecutor
):
one = (workspace / "one.py").resolve()
with one.open("w") as fobj:
@ -1792,7 +1792,7 @@ def test_write_cache_creates_directory_if_needed(self) -> None:
def test_failed_formatting_does_not_get_cached(self) -> None:
mode = DEFAULT_MODE
with cache_dir() as workspace, patch(
"black.ProcessPoolExecutor", new=ThreadPoolExecutor
"concurrent.futures.ProcessPoolExecutor", new=ThreadPoolExecutor
):
failing = (workspace / "failing.py").resolve()
with failing.open("w") as fobj: