Skip to content

fix: support lazy imports in Python 3.15+ - #1129

Open
henryiii wants to merge 3 commits into
pypa:mainfrom
henryiii:henryiii/fix/lazy
Open

henryiii wants to merge 3 commits into
pypa:mainfrom
henryiii:henryiii/fix/lazy

Conversation

@henryiii

@henryiii henryiii commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

This is the output of uvx flake8-lazy --apply src/**.py. See post: https://iscinumpy.dev/post/flake8-lazy

Opening this as draft for awareness and to get eyes on the changes; it's
possible I've forgotten some or added too many. Probably should wait to put
this in until 3.15b1. It's easy to rerun if there are conflicts in the future.

I don't really know how to test this, other than running a python process and checking to see what's in sys.modules (which I did locally), or checking -Ximporttime, etc. We don't have a CLI (yet?)) to check import times on.

@notatallshaw

Copy link
Copy Markdown
Member

FYI, I'm adding a mechanism in pip where we will prevent imports from running after a certain point, before we vendor libraries that use lazy imports we will also need to add a mechanism to resolve all lazy imports and import them: pypa/pip#13829

This is not a blocker for packaging, we vendor packaging and can make sure one doesn't land without the other. But I do believe my approach is best practices for all Python package installers that use the same Python environment they are installing into, so once this lands on pip side I do think we should communicate this to known Python packaging installers that use packaging, before packaging adopts lazy imports.

@henryiii

Copy link
Copy Markdown
Contributor Author

It seems like a "resolve all lazy imports" function would be really useful - package installers could resolve them all right before installing packages, and still get the benefits of lazy imports for things like --help.

The vendoring tool could learn to remove lazy and __lazy_modules__. Or a tool could strip them from all code (ruff does have a check to disallow them, I think).

Comment thread src/packaging/version.py Resolved
@henryiii henryiii mentioned this pull request Oct 30, 2026
@notatallshaw

Copy link
Copy Markdown
Member

FYI, I'm now +1 on lazy modules, pip is ready.

@henryiii

Copy link
Copy Markdown
Contributor Author

Two updates (besides the bump in flake8-lazy): this is now maintained by a pre-commit hook, and I'm using the set style, which is faster if there are more than 1-2 entries (checked by __contains__).

@henryiii
henryiii marked this pull request as ready for review October 30, 2026 20:27
@henryiii

henryiii commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

I’ll rebase. Are we fine with the two changes (set and pre-commit hook)?

Generated with flake8-lazy 0.10.1 (`--apply list`).

Assisted-by: ClaudeCode:claude-opus-5-5
Generated with flake8-lazy 0.10.1 (`--apply set`).

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5

This branch has not been deployed

No deployments
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.

4 participants