Skip to content

gh-148588: Document __lazy_modules__#148590

Merged
pablogsal merged 17 commits intopython:mainfrom
johnslavik:gh-148588-document-lazy-modules
Apr 25, 2026
Merged

gh-148588: Document __lazy_modules__#148590
pablogsal merged 17 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>
@johnslavik johnslavik requested a review from hugovk April 18, 2026 17:32
Comment thread Doc/reference/simple_stmts.rst Outdated
Copy link
Copy Markdown
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

Small nit

johnslavik and others added 3 commits April 24, 2026 21:47
@pablogsal pablogsal merged commit 9dab866 into python:main Apr 25, 2026
30 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Apr 25, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news topic-lazy-imports

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants