fix: support lazy imports in Python 3.15+#1129
Conversation
|
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. |
|
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 The vendoring tool could learn to remove |
|
|
||
| from __future__ import annotations | ||
|
|
||
| __lazy_modules__ = ["functools", "warnings"] |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
This is the output of
uvx flake8-lazy --apply src/**.py. See post: https://iscinumpy.dev/post/flake8-lazyOpening 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.