Skip to content

Conversation

@pablogsal
Copy link
Member

@pablogsal pablogsal commented Dec 6, 2025

⚠️ Dear reviewers, to avoid collapsing the GitHub API with a lot of comments, please open PRs against the base branch with any suggestions or fixes if you are sure are not controversial ⚠️


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

DinoV and others added 30 commits October 2, 2025 13:22
Highlight lazy imports in the new REPL
@python-cla-bot
Copy link

python-cla-bot bot commented Dec 15, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

brittanyrey and others added 6 commits December 15, 2025 10:45
* Update example to add missing import
* Add link to lazy keyword
* Update documentation
* PEP-7 for import.c

* re-order imports

* fix tests

* some final nits

* alter other file

* alter sysmodule.c
…ces,

and prefer using `//` for comments instead of `/* */`.
.. c:function:: int PyImport_SetLazyImportsMode(PyImport_LazyImportsMode mode)
Similar to :c:func:`PyImport_ImportModuleAttr`, but names are UTF-8 encoded
Copy link
Member

Choose a reason for hiding this comment

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

Copy-paste error? This seems unrelated to the function being described.


print('json' in sys.modules) # True - now loaded

If an error occurs during module loading (such as :exc:`ImportError` or
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps also document that ImportCycleError may be raised?

1 LOAD_SMALL_INT 0
LOAD_CONST 1 (('*',))
IMPORT_NAME 0 (math)
IMPORT_NAME 2 (math + eager)
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding more tests for the new logic in dis.py.

try:
lazy import os
except:
pass
Copy link
Member

Choose a reason for hiding this comment

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

What about putting it in the except:?

Choose a reason for hiding this comment

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

Added a commit to address

lazy import within an except block. Also removed unused var.
Comment on lines +885 to +892
The :keyword:`lazy` keyword marks an import as lazy. It is a :ref:`soft keyword
<soft-keywords>` that only has special meaning when it appears immediately
before an :keyword:`import` or :keyword:`from` statement.

When an import statement is preceded by the :keyword:`lazy` keyword,
the import becomes *lazy*: the module is not loaded immediately at the import
statement. Instead, a lazy proxy object is created and bound to the name. The
actual module is loaded on first use of that name.
Copy link

@javabster javabster Dec 23, 2025

Choose a reason for hiding this comment

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

I would recommend swapping the first two paragraphs around, the first sentence "the lazy keyword marks an import as lazy" is not super helpful if you don't define what you mean by "lazy" until the next paragraph 😅

Suggested change
The :keyword:`lazy` keyword marks an import as lazy. It is a :ref:`soft keyword
<soft-keywords>` that only has special meaning when it appears immediately
before an :keyword:`import` or :keyword:`from` statement.
When an import statement is preceded by the :keyword:`lazy` keyword,
the import becomes *lazy*: the module is not loaded immediately at the import
statement. Instead, a lazy proxy object is created and bound to the name. The
actual module is loaded on first use of that name.
When an import statement is preceded by the :keyword:`lazy` keyword,
the import becomes *lazy*: the module is not loaded immediately at the import
statement. Instead, a lazy proxy object is created and bound to the name. The
actual module is loaded on first use of that name.
The :keyword:`lazy` keyword is a :ref:`soft keyword
<soft-keywords>` that only has special meaning when it appears immediately
before an :keyword:`import` or :keyword:`from` statement.

Choose a reason for hiding this comment

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

Alternatively, you could keep the same order of the paragraphs but just remove the first sentence

Comment on lines +106 to +108
first used. This gives you the organizational benefits of declaring all
imports at the top of the file while only paying the loading cost for
modules you actually use.

Choose a reason for hiding this comment

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

I think this is a good succinct point explaining why folks would want to use lazy imports - might be worth also mentioning this reasoning in the reference section

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.