Skip to content

Speed up setuptools imports on Python 3.15+ by adding __lazy_modules__#5257

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

Speed up setuptools imports on Python 3.15+ by adding __lazy_modules__#5257
Avasam wants to merge 2 commits into
pypa:mainfrom
Avasam:Python-3.15-`__lazy_modules__`

Conversation

@Avasam

@Avasam Avasam commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Summary of changes

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

Once pypa/distutils#414 is also merged, this will

Autofixed by running

import glob, os, subprocess, sys
skip = {'tests', '_distutils', '_vendor', '_validate_pyproject'}
files = [f for f in glob.glob('setuptools/**/*.py', recursive=True) if not skip & set(f.split(os.sep))]
subprocess.check_call([sys.executable, '-m', 'flake8_lazy', *files, '--apply=set', '--strict-typing'])

Note: Merging #5222 first should reduce import reification related errors found by test.

Pull Request Checklist

@mergify

mergify Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@Avasam Avasam force-pushed the Python-3.15-`__lazy_modules__` branch from 2cf37a2 to 356bcc8 Compare July 11, 2026 18:45
@Avasam Avasam changed the title Python 3.15 __lazy_modules__ (auto-applied w/ flake8-lazy --apply=set) Speed up setuptools imports on Python 3.15+ by adding __lazy_modules__ Jul 11, 2026
Comment thread pyproject.toml

# Deprecated rules must now be selected by exact rule code
"ruff >= 0.13.0; 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
Comment on lines +10 to +17
# flake8-lazy has no pytest plugin; run its CLI over the package sources.
# tox doesn't shell-glob, so expand the file list cross-platform via Python
# and drop test dirs (lazy-import hygiene is irrelevant there) plus the
# vendored/distutils trees (maintained upstream, not our lazy-import concern).
python -c "import glob, os, subprocess, sys; \
skip = {'tests', '_distutils', '_vendor', _validate_pyproject}; \
files = [f for f in glob.glob('setuptools/**/*.py', recursive=True) if not skip & set(f.split(os.sep))]; \
subprocess.check_call([sys.executable, '-m', 'flake8_lazy', *files])"

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 force-pushed the Python-3.15-`__lazy_modules__` branch from 356bcc8 to 3d3bbc0 Compare July 11, 2026 18:54
@Avasam Avasam force-pushed the Python-3.15-`__lazy_modules__` branch from 3d3bbc0 to a17d55e Compare July 11, 2026 18:59
@Avasam Avasam force-pushed the Python-3.15-`__lazy_modules__` branch from 41b2d76 to be82d65 Compare July 11, 2026 22:11
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.

[BUG] SETUPTOOLS_USE_DISTUTILS=local makes any distutils import very slow [BUG] Importing setuptools is slow and takes nearly half a second

1 participant