Skip to content

Support automatic worker selection - #21983

Merged
ilevkivskyi merged 16 commits into
python:masterfrom
KevinRK29:add-auto-workers
Sep 16, 2026
Merged

ilevkivskyi merged 16 commits into
python:masterfrom
KevinRK29:add-auto-workers

Conversation

@KevinRK29

@KevinRK29 KevinRK29 commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Fixes #21477

Allow auto anywhere the number of parallel workers can be configured.

The auto selection reuses get_available_threads and is capped at 8 workers. You can still exceed the cap by specifying the number of workers.

Also 0 still disables parallel checking, 1 selects one worker, and the default remains 0.

Also this displays a warning when incremental mode is disabled (when using parallel checking)

@KevinRK29 KevinRK29 changed the title Add auto workers Support automatic worker selection Sep 14, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@KevinRK29
KevinRK29 marked this pull request as ready for review September 15, 2026 07:08
@github-actions

This comment has been minimized.

@KevinRK29

Copy link
Copy Markdown
Collaborator Author

hmm, seems like because the CI doesn't like that im using these mocks

Comment on lines +1022 to +1023
The ``MYPY_NUM_WORKERS`` environment variable accepts the same values and
overrides this setting.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of precedence is: command line flag > environment variable > configuration file; yes? Does that match the other options?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this is the standard order for mypy.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it follows the cache_dir behaviour

Comment thread mypy/test/testargs.py
from io import StringIO
from pathlib import Path
from typing import Any, cast
from unittest import mock

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, use pytest.monkeypatch instead of unittest; though neither can mock mypyc compiled code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @KevinRK29 mypyc doesn't support monkey patching. Instead, you can try setting (private) cached value like this:

mypy.util._AVAILABLE_THREADS = 32
# <...testing...>
mypy.util._AVAILABLE_THREADS = None

You can even write a simple context manager to do this (but do not expose it, keep it private to this test file)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevinRK29 to be clear mock.patch.dict is fine, the problem is only with trying to patch a function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, i've updated it to use the context manager approach

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, thanks! I have few comments.

Comment thread docs/source/command_line.rst Outdated
Comment thread docs/source/command_line.rst Outdated
* Parallel mode requires and automatically enables :option:`--native-parser`.
* Parallel mode requires and automatically enables :option:`--native-parser`
and :ref:`incremental mode <incremental>`. Specifying
:option:`--no-incremental` has no effect in parallel mode.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more about this, and silently overriding it may be not the best thing. People may need it for some niche things like running benchmarks. So we should respect it, but show a warning instead. More on this below.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea that makes sense to me

Comment thread mypy/main.py Outdated
if options.num_workers:
# Supporting both parsers would be really tricky, so just support the new one.
options.native_parser = True
options.incremental = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do something like this here instead:

if not options.incremental and os.path.isdir(options.cache_dir):
    print("Warning: disabling incremental mode may severely reduce performance")
    print(f"If this is intentional, delete '{options.cache_dir}' to suppress this warning")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used this approach

Comment thread mypy/main.py Outdated
metavar="VALUE",
default=0,
help="Number of separate mypy worker processes (experimental)",
help="Number of separate mypy worker processes, or 'auto' (experimental)",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to say that parallel type checking is experimental anymore.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the experimental wording

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is ready now.

@ilevkivskyi
ilevkivskyi merged commit 4d195a2 into python:master Sep 16, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect number of CPUs with --num-workers auto

3 participants