Skip to content

gh-142349: Move all lazy-imports-by-hand to use the new lazy keyword#144894

Draft
pablogsal wants to merge 1 commit intopython:mainfrom
pablogsal:unlazy
Draft

gh-142349: Move all lazy-imports-by-hand to use the new lazy keyword#144894
pablogsal wants to merge 1 commit intopython:mainfrom
pablogsal:unlazy

Conversation

@pablogsal
Copy link
Member

@pablogsal pablogsal commented Feb 16, 2026

@bedevere-app bedevere-app bot mentioned this pull request Feb 16, 2026
@pablogsal
Copy link
Member Author

@Yhg1s @DinoV WDYT?

@DavidCEllis
Copy link
Contributor

Maybe worth noting that this would make -X lazy_imports=none unusable as most of the standard library will fail to import.

@pablogsal
Copy link
Member Author

pablogsal commented Feb 17, 2026

Maybe worth noting that this would make -X lazy_imports=none unusable as most of the standard library will fail to import.

Not fail to import, it will just be slower (if properly done).

@pablogsal
Copy link
Member Author

To be honest I am not super into this idea myself, but wanted to show how it looks for debate

@DavidCEllis
Copy link
Contributor

Maybe worth noting that this would make -X lazy_imports=none unusable as most of the standard library will fail to import.

Not fail to import, it will just be slower

You changed this to add (if properly done), but to be clear.

A couple of examples with this PR:

$ ./python -X lazy_imports=none -c "import re"
...
ImportError: cannot import name 'partial' from partially initialized module 'functools' (most likely due to a circular import) (/home/david/src/cpython/Lib/functools.py)
$ ./python -X lazy_imports=none -c "import collections"
...
ImportError: cannot import name 'deepcopy' from partially initialized module 'copy' (most likely due to a circular import) (/home/david/src/cpython/Lib/copy.py)

I do think in general the new lazy import form looks better, but it will need to be done with care (are there already tests to make sure the stdlib is still importable under lazy_imports=none?).

@pablogsal
Copy link
Member Author

pablogsal commented Feb 17, 2026

You changed this to add (if properly done), but to be clear.

Yeah :) What I wanted to say is that in theory just moving these imports to top level should not fail (as opposed to imports in with statements or try/excepts) if properly done

A couple of examples with this PR:

Thanks for the examples! Some extra passes need to be done if we want it because as it is test are failing anyway :(

(are there already tests to make sure the stdlib is still importable under lazy_imports=none?).

Not (yet) since we haven't started to use the feature around.

Thanks a lot for the point, this is indeed an important aspect that we must not overlook here or elsewhere!

@DavidCEllis
Copy link
Contributor

I picked re and collections as examples as they frequently end up somewhere in the chain of imports. functools is another that fails. I wasn't kidding when I said most of the standard library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants