Skip to content

Speed up distutils imports on Python 3.15+ by adding __lazy_modules__ - #414

Open
Avasam wants to merge 3 commits into
pypa:mainfrom
Avasam:Python-3.15-`__lazy_modules__`
Open

Avasam wants to merge 3 commits into
pypa:mainfrom
Avasam:Python-3.15-`__lazy_modules__`

Conversation

@Avasam

@Avasam Avasam commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Alternative dynamic mode: https://flake8-lazy.readthedocs.io/en/latest/cli/#dynamic-mode

Works towards:

The same changes would have to be done on setuptools side to close those issues. (pypa/setuptools#5257)

Autofixed by running

import glob, os, subprocess, sys;
files = [f for f in glob.glob('distutils/**/*.py', recursive=True) if 'tests' not in f.split(os.sep)];
subprocess.check_call([sys.executable, '-m', 'flake8_lazy', '--strict-typing', *files, '--apply=set'])

Comment thread .flake8
@@ -1,9 +0,0 @@
[flake8]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Leftover unused flake8 file

Comment thread pyproject.toml
check = [
"pytest-checkdocs >= 2.14",
"pytest-ruff >= 0.2.1; sys_platform != 'cygwin'",
"flake8-lazy" # Until Ruff gains support for automatically inserting and validating __lazy_modules__

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread tox.ini
# tox doesn't shell-glob, so expand the file list cross-platform via Python
# and drop test dirs (lazy-import hygiene is irrelevant there).
python -c "import glob, os, subprocess, sys; \
files = [f for f in glob.glob('distutils/**/*.py', recursive=True) if 'tests' not in f.split(os.sep)]; \

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This could be massively simplified with recursive globing support: henryiii/flake8-lazy#92

@Avasam Avasam changed the title Python 3.15 __lazy_modules__ (auto-applied w/ flake8-lazy --apply=set) Speed up distutils imports on Python 3.15+ by adding __lazy_modules__ Jul 11, 2026
@Avasam
Avasam force-pushed the Python-3.15-`__lazy_modules__` branch from b2a0867 to 684b732 Compare July 11, 2026 18:58
@Avasam
Avasam force-pushed the Python-3.15-`__lazy_modules__` branch 3 times, most recently from 9e37fee to d84d61d Compare August 3, 2026 20:29
@Avasam
Avasam force-pushed the Python-3.15-`__lazy_modules__` branch from d84d61d to 3d7b28f Compare September 10, 2026 19:35
flake8-lazy ships no pytest plugin, so invoke its CLI from the primary
Avoid test dirs where lazy-import hygiene is irrelevant (if easy to exclude).
@Avasam
Avasam force-pushed the Python-3.15-`__lazy_modules__` branch from 3d7b28f to 0cbc7e5 Compare September 10, 2026 19:36
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.

1 participant