Skip to content

Support automatic worker selection - #21983

Open
KevinRK29 wants to merge 10 commits into
python:masterfrom
KevinRK29:add-auto-workers
Open

KevinRK29 wants to merge 10 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 automatically enables incremental mode whenever parallel checking is enabled.

@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

Copy link
Copy Markdown
Contributor

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

@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.

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.

@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 on lines +1057 to +1058
The ``MYPY_NUM_WORKERS`` environment variable also accepts ``auto``. This
setting will override the environment variable if it is set.

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 you need to add anything about MYPY_NUM_WORKERS here, just keep the original text.

Suggested change
The ``MYPY_NUM_WORKERS`` environment variable also accepts ``auto``. This
setting will override the environment variable if it is set.
This setting will override the ``MYPY_NUM_WORKERS`` environment
variable if it is set.

* 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.

Comment thread mypy/main.py
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")

Comment thread mypy/main.py
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.

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