[mypyc] Import setuptools lazily in mypyc.build on Python 3.12+ - #22084
Merged
Merged
Conversation
Importing mypyc.build used to always import setuptools, distutils and mypyc.build_setup, but mypyc_build (type checking and C generation) needs none of them. They are only needed for building extensions, e.g. in mypycify. Importing them is slow, which adds significant fixed overhead to tools that call mypyc_build directly for many small builds, such as Bazel integrations. Move these imports into import_distutils(), called by the functions that need them. On Python versions before 3.12, distutils is still in the stdlib and setuptools must be imported before it to override it, so keep importing everything eagerly there to preserve backward compatibility. In a quick local simulation (interpreted mypy, Python 3.14, warm caches, tiny module, calling mypyc_build like a Bazel driver), this saved about 31-37 ms of a ~190 ms run (16-19%). The number of setuptools-related modules imported went from 102 to 3.
- The module docstring overstated the distutils fallback: mypycify already requires setuptools (via the copy_extensions_to_source patch). - librt_cache relied on importing mypyc.build to set up distutils, which is no longer true on 3.12+. Use import_distutils() explicitly. - Add a subprocess test checking that C generation via mypyc_build doesn't import setuptools, distutils or mypyc.build_setup.
p-sawicki
approved these changes
Oct 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Importing mypyc.build used to always import setuptools, distutils and
mypyc.build_setup, but mypyc_build (type checking and C generation) needs
none of them. They are only needed for building extensions, e.g. in
mypycify. Importing them is slow, which adds significant fixed overhead
to tools that call mypyc_build directly for many small builds, such as
Bazel integrations.
Move these imports into a helper function, called by the functions that
need them. On Python versions before 3.12, distutils is still in the
stdlib and setuptools must be imported before it to override it, so keep
the eager imports there to preserve backward compatibility.
In an interpreted microbenchmark this saved ~34 ms of a ~190 ms run (17%).