Skip to content

gh-148588: Document __lazy_modules__#148590

Open
johnslavik wants to merge 14 commits intopython:mainfrom
johnslavik:gh-148588-document-lazy-modules
Open

gh-148588: Document __lazy_modules__#148590
johnslavik wants to merge 14 commits intopython:mainfrom
johnslavik:gh-148588-document-lazy-modules

Conversation

@johnslavik
Copy link
Copy Markdown
Member

@johnslavik johnslavik commented Apr 15, 2026

I'll need some info here, cc @pablogsal @Yhg1s

The PEP reads as if __lazy_modules__ must implement __contains__. The actual implementation supports any iterable, because it uses the classic membership test.

Question: Is it an implementation detail of CPython to support objects beyond containers in __lazy_modules__, and non-containers aren't supported, or do we support any iterable for __lazy_modules__?

Right now I can assign a generator or an old-style __getitem__-based sequence to __lazy_modules__ and CPython will walk it to check membership. Question is whether it's just a CPython thing.

EDIT: ...Hmmm now that I think about it, this makes no sense: a __lazy_modules__ generator will be empty after the first import, rendering all subsequent imports eager. I believe we shouldn't support anything beyond containers. Maybe we could even do this at the runtime level, but maybe if you put a generator to __lazy_modules__ you deserve anything that happens to you.


📚 Documentation preview 📚: https://cpython-previews--148590.org.readthedocs.build/

…y imports

Add documentation for the __lazy_modules__ module attribute in two places:

- Doc/reference/simple_stmts.rst: new subsection under the lazy imports
  section explaining the compatibility mode, its semantics (module-scope
  only, overridden by -X lazy_imports=none), and a usage example.

- Doc/reference/datamodel.rst: new module.__lazy_modules__ attribute entry
  with a cross-reference to the detailed explanation in simple_stmts.

Also extend the What's New in Python 3.15 lazy-imports entry with a short
paragraph and example demonstrating __lazy_modules__.
… docs entry

Replace bare __lazy_modules__ in the 3.15.0a8 NEWS entry with
:attr:`module.__lazy_modules__` to link to the new attribute documentation.
Remove the separate docs NEWS entry; the archive entry serves as the record.
Replace 'migrating a large existing codebase' with the more accurate
use case: supporting Python <3.15 while leveraging lazy imports on 3.15+.
Comment thread Doc/whatsnew/3.15.rst Outdated
Comment thread Misc/NEWS.d/3.15.0a8.rst Outdated
Comment thread Misc/NEWS.d/3.15.0a8.rst Outdated
Comment thread Doc/reference/simple_stmts.rst Outdated
Comment thread Doc/reference/simple_stmts.rst Outdated
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants