From 12898c21df9f3311ddfd843e2c0cff5957b5219a Mon Sep 17 00:00:00 2001 From: Kevin Kannammalil Date: Mon, 4 May 2026 15:14:04 -0400 Subject: [PATCH 1/3] add env variable for configuring --num-workers --- docs/source/command_line.rst | 3 +++ docs/source/config_file.rst | 2 ++ mypy/main.py | 13 +++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index c1fd4ad6446b..b8e4c7c187ba 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -1052,6 +1052,9 @@ enabled by default starting from mypy 2.0. disables parallel checking. Automatic detection of the optimal number of workers is not supported yet. + This setting will override the ``MYPY_NUM_WORKERS`` environment + variable if it is set. + Notes: * An import cycle is always processed as a whole by a worker process. Thus, diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 4f2fddaeba8c..d5431b1476aa 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -1018,6 +1018,8 @@ These options may only be set in the global section (``[mypy]``). Use specific number of parallel worker processes for type-checking, see :ref:`parallel type-checking ` for more details. + This setting will be overridden by the ``MYPY_NUM_WORKERS`` environment + variable. Advanced options diff --git a/mypy/main.py b/mypy/main.py index 040275984fb6..e459c83704ea 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -393,7 +393,8 @@ def infer_python_executable(options: Options, special_opts: argparse.Namespace) FOOTER: Final = """Environment variables: Define MYPYPATH for additional module search path entries. - Define MYPY_CACHE_DIR to override configuration cache_dir path.""" + Define MYPY_CACHE_DIR to override configuration cache_dir path. + Define MYPY_NUM_WORKERS to override configuration num_workers value.""" class CapturableArgumentParser(argparse.ArgumentParser): @@ -1183,7 +1184,10 @@ def add_invertible_flag( "--num-workers", type=int, default=0, - help="Number of separate mypy worker processes (experimental)", + help=( + "Number of separate mypy worker processes (experimental). " + "Overrides the MYPY_NUM_WORKERS environment variable if set." + ), ) report_group = parser.add_argument_group( @@ -1465,6 +1469,11 @@ def set_strict_flags() -> None: options.cache_dir = environ_cache_dir options.cache_dir = os.path.expanduser(options.cache_dir) + # Override num_workers if provided in the environment + environ_num_workers = os.getenv("MYPY_NUM_WORKERS", "").strip() + if environ_num_workers: + options.num_workers = int(environ_num_workers) + # Parse command line for real, using a split namespace. special_opts = argparse.Namespace() parser.parse_args(args, SplitNamespace(options, special_opts, "special-opts:")) From 7418d86a86000d31265413c4b9a3422ee017451e Mon Sep 17 00:00:00 2001 From: Kevin Kannammalil Date: Mon, 4 May 2026 16:17:07 -0400 Subject: [PATCH 2/3] add input validation --- mypy/main.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index e459c83704ea..9bd67a521e85 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1184,10 +1184,7 @@ def add_invertible_flag( "--num-workers", type=int, default=0, - help=( - "Number of separate mypy worker processes (experimental). " - "Overrides the MYPY_NUM_WORKERS environment variable if set." - ), + help="Number of separate mypy worker processes (experimental)", ) report_group = parser.add_argument_group( @@ -1470,9 +1467,14 @@ def set_strict_flags() -> None: options.cache_dir = os.path.expanduser(options.cache_dir) # Override num_workers if provided in the environment - environ_num_workers = os.getenv("MYPY_NUM_WORKERS", "").strip() - if environ_num_workers: - options.num_workers = int(environ_num_workers) + environ_num_workers = os.getenv("MYPY_NUM_WORKERS", "") + if environ_num_workers.strip(): + try: + options.num_workers = int(environ_num_workers) + except ValueError: + parser.error( + f"MYPY_NUM_WORKERS must be an integer, got {environ_num_workers!r}" + ) # Parse command line for real, using a split namespace. special_opts = argparse.Namespace() From aa29d277d806948a510f51a525ec42a930566a39 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 20:39:35 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 9bd67a521e85..9227e24680ce 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1472,9 +1472,7 @@ def set_strict_flags() -> None: try: options.num_workers = int(environ_num_workers) except ValueError: - parser.error( - f"MYPY_NUM_WORKERS must be an integer, got {environ_num_workers!r}" - ) + parser.error(f"MYPY_NUM_WORKERS must be an integer, got {environ_num_workers!r}") # Parse command line for real, using a split namespace. special_opts = argparse.Namespace()