Skip to content

Commit d091daa

Browse files
Add support for configuring --num-workers with a env variable (#21407)
Fixes #21395 Specify num workers env var and added documentation for it. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0ea16df commit d091daa

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

docs/source/command_line.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,9 @@ enabled by default starting from mypy 2.0.
10521052
disables parallel checking. Automatic detection of the optimal number
10531053
of workers is not supported yet.
10541054

1055+
This setting will override the ``MYPY_NUM_WORKERS`` environment
1056+
variable if it is set.
1057+
10551058
Notes:
10561059

10571060
* An import cycle is always processed as a whole by a worker process. Thus,

docs/source/config_file.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ These options may only be set in the global section (``[mypy]``).
10181018

10191019
Use specific number of parallel worker processes for type-checking, see
10201020
:ref:`parallel type-checking <parallel>` for more details.
1021+
This setting will be overridden by the ``MYPY_NUM_WORKERS`` environment
1022+
variable.
10211023

10221024

10231025
Advanced options

mypy/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ def infer_python_executable(options: Options, special_opts: argparse.Namespace)
393393

394394
FOOTER: Final = """Environment variables:
395395
Define MYPYPATH for additional module search path entries.
396-
Define MYPY_CACHE_DIR to override configuration cache_dir path."""
396+
Define MYPY_CACHE_DIR to override configuration cache_dir path.
397+
Define MYPY_NUM_WORKERS to override configuration num_workers value."""
397398

398399

399400
class CapturableArgumentParser(argparse.ArgumentParser):
@@ -1465,6 +1466,14 @@ def set_strict_flags() -> None:
14651466
options.cache_dir = environ_cache_dir
14661467
options.cache_dir = os.path.expanduser(options.cache_dir)
14671468

1469+
# Override num_workers if provided in the environment
1470+
environ_num_workers = os.getenv("MYPY_NUM_WORKERS", "")
1471+
if environ_num_workers.strip():
1472+
try:
1473+
options.num_workers = int(environ_num_workers)
1474+
except ValueError:
1475+
parser.error(f"MYPY_NUM_WORKERS must be an integer, got {environ_num_workers!r}")
1476+
14681477
# Parse command line for real, using a split namespace.
14691478
special_opts = argparse.Namespace()
14701479
parser.parse_args(args, SplitNamespace(options, special_opts, "special-opts:"))

0 commit comments

Comments
 (0)