Skip to content

Commit 7760563

Browse files
authored
Expose --num-workers and --native-parser (#21387)
Three things here: * Add the docs for `--num-workers` and `--native-parser` * Remove `argparse.SUPPRESS` from them * Add couple checks for `num_workers`
1 parent 9e2d061 commit 7760563

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

docs/source/command_line.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,40 @@ beyond what incremental mode can offer, try running mypy in
10321032
Skip cache internal consistency checks based on mtime.
10331033

10341034

1035+
.. _parallel:
1036+
1037+
Parallel type-checking
1038+
**********************
1039+
1040+
By default, mypy checks all modules in the same Python process. This can be slow
1041+
for large code bases. Mypy offers experimental parallel type-checking mode using
1042+
multiple worker processes. In parallel mode, modules that do not depend om each
1043+
other are type-checked in parallel. :ref:`Incremental cache <incremental>` is
1044+
used to manage most of the shared state. Parallel type-checking also requires
1045+
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is
1046+
enabled by default starting from mypy 2.0.
1047+
1048+
.. option:: -n NUMBER, --num-workers NUMBER
1049+
1050+
Use ``NUMBER`` parallel worker processes (in addition to the coordinator
1051+
process) to perform type-checking. Specifying ``--num-workers 0`` (default)
1052+
disables parallel checking. Automatic detection of the optimal number
1053+
of workers is not supported yet.
1054+
1055+
Notes:
1056+
1057+
* An import cycle is always processed as a whole by a worker process. Thus,
1058+
avoiding large import cycles in your code will *significantly* improve
1059+
type-checking speed.
1060+
1061+
* Specifying a number of workers that is larger than the number of *physical*
1062+
CPU cores is not beneficial, since mypy is usually CPU bound. Best way to
1063+
tune the number of workers on a given machine is to start from 3-4 workers
1064+
and increase the number while you see a performance improvement.
1065+
1066+
* Parallel mode requires and automatically enables :option:`--native-parser`.
1067+
1068+
10351069
Advanced options
10361070
****************
10371071

@@ -1105,6 +1139,11 @@ in developing or debugging mypy internals.
11051139
cause mypy to type check the contents of ``temp.py`` instead of ``original.py``,
11061140
but error messages will still reference ``original.py``.
11071141

1142+
.. option:: --native-parser
1143+
1144+
This enables fast Rust-based parser that parses directly to mypy AST.
1145+
It will become the default parser in one of the next mypy releases.
1146+
11081147

11091148
Report generation
11101149
*****************

docs/source/config_file.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,20 @@ These options may only be set in the global section (``[mypy]``).
10061006
Skip cache internal consistency checks based on mtime.
10071007

10081008

1009+
Parallel type-checking configuration
1010+
************************************
1011+
1012+
These options may only be set in the global section (``[mypy]``).
1013+
1014+
.. confval:: num_workers
1015+
1016+
:type: integer
1017+
:default: 0
1018+
1019+
Use specific number of parallel worker processes for type-checking, see
1020+
:ref:`parallel type-checking <parallel>` for more details.
1021+
1022+
10091023
Advanced options
10101024
****************
10111025

@@ -1067,6 +1081,14 @@ These options may only be set in the global section (``[mypy]``).
10671081
Warns about missing type annotations in typeshed. This is only relevant
10681082
in combination with :confval:`disallow_untyped_defs` or :confval:`disallow_incomplete_defs`.
10691083

1084+
.. confval:: native_parser
1085+
1086+
:type: boolean
1087+
:default: False
1088+
1089+
This enables fast Rust-based parser that parses directly to mypy AST.
1090+
It will become the default parser in one of the next mypy releases.
1091+
10701092

10711093
Report generation
10721094
*****************

mypy/main.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ def main(
100100
if options.num_workers:
101101
# Supporting both parsers would be really tricky, so just support the new one.
102102
options.native_parser = True
103+
if options.num_workers < 0:
104+
fail("error: Number of workers cannot be negative", stderr, options)
103105
if options.cache_dir == os.devnull:
104106
fail("error: Cache must be enabled in parallel mode", stderr, options)
107+
if not options.local_partial_types:
108+
fail("error: --local-partial-types must be enabled in parallel mode", stderr, options)
105109
if options.report_dirs:
106110
fail(
107111
"error: Reports are not supported in parallel mode yet\n"
@@ -1175,7 +1179,11 @@ def add_invertible_flag(
11751179

11761180
# Experimental parallel type-checking support.
11771181
internals_group.add_argument(
1178-
"-n", "--num-workers", type=int, default=0, help=argparse.SUPPRESS
1182+
"-n",
1183+
"--num-workers",
1184+
type=int,
1185+
default=0,
1186+
help="Number of separate mypy worker processes (experimental)",
11791187
)
11801188

11811189
report_group = parser.add_argument_group(
@@ -1288,7 +1296,11 @@ def add_invertible_flag(
12881296
help=argparse.SUPPRESS,
12891297
)
12901298
# --native-parser enables the native parser (experimental)
1291-
add_invertible_flag("--native-parser", default=False, help=argparse.SUPPRESS)
1299+
add_invertible_flag(
1300+
"--native-parser",
1301+
default=False,
1302+
help="Enable faster parser that parses directly to mypy AST",
1303+
)
12921304
# --logical-deps adds some more dependencies that are not semantically needed, but
12931305
# may be helpful to determine relative importance of classes and functions for overall
12941306
# type precision in a code base. It also _removes_ some deps, so this flag should be never

0 commit comments

Comments
 (0)