Skip to content

fix: support lazy imports in Python 3.15+#1129

Draft
henryiii wants to merge 1 commit intopypa:mainfrom
henryiii:henryiii/fix/lazy
Draft

fix: support lazy imports in Python 3.15+#1129
henryiii wants to merge 1 commit intopypa:mainfrom
henryiii:henryiii/fix/lazy

Conversation

@henryiii
Copy link
Copy Markdown
Contributor

@henryiii henryiii commented Mar 24, 2026

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


from __future__ import annotations

__lazy_modules__ = ["functools", "warnings"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

functools are only imported for python <3.13 so having it here seems pointless?
Also is having warnings here really worth it, it's a tiny dependency and very likely imported from elsewhere anyway...

Copy link
Copy Markdown
Contributor Author

@henryiii henryiii Mar 25, 2026

Choose a reason for hiding this comment

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

It's automated based on rules - if it's not used at top level, it shows up here. (https://flake8-lazy.readthedocs.io). Not sure it's worth adding complexity for the python version case.

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