From 67604a3b5032ecffc73125635fa5c7fdecb48ddd Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Thu, 30 Jul 2026 12:24:44 +0200 Subject: [PATCH 1/2] Add num-workers auto option --- docs/source/command_line.rst | 4 ++-- mypy/main.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index b8e4c7c187ba4..d87af8103805a 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -1049,8 +1049,8 @@ enabled by default starting from mypy 2.0. Use ``NUMBER`` parallel worker processes (in addition to the coordinator process) to perform type-checking. Specifying ``--num-workers 0`` (default) - disables parallel checking. Automatic detection of the optimal number - of workers is not supported yet. + disables parallel checking. Automatic detection of the physical CPU core count + is possible through ``--num-workers auto```. This setting will override the ``MYPY_NUM_WORKERS`` environment variable if it is set. diff --git a/mypy/main.py b/mypy/main.py index 0cf624a3a5d7b..54ad12d9d97f7 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse +import multiprocessing import os import platform import subprocess @@ -1179,12 +1180,18 @@ def add_invertible_flag( internals_group.add_argument("--export-ref-info", action="store_true", help=argparse.SUPPRESS) # Experimental parallel type-checking support. + def _num_workers_type(value: str) -> int: + if value == "auto": + return multiprocessing.cpu_count() + + return int(value) + internals_group.add_argument( "-n", "--num-workers", - type=int, + type=_num_workers_type, default=0, - help="Number of separate mypy worker processes (experimental)", + help="Number of separate mypy worker processes (experimental). With `auto` detects (and uses) physical CPU count.", ) report_group = parser.add_argument_group( From f8d953f21eaabd4425d56dcd897d02ae67bcaa45 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Thu, 30 Jul 2026 12:28:39 +0200 Subject: [PATCH 2/2] Add num-workers auto test --- test-data/unit/cmdline.test | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 7066034b3e39c..20e06bafefa23 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -1321,3 +1321,13 @@ error: Cache must be enabled in parallel mode from foo.api import bar as bar [file foo-stubs/api/bar.pyi] [out] + +[case testParallelAutoRunWithSyntaxError] +# cmd: mypy a.py --num-workers=auto --pretty +[file a.py] +1 2 +[out] +a.py:1: error: Simple statements must be separated by newlines or semicolons + 1 2 + ^ +== Return code: 2