Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion peps/pep-0810.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Author: Pablo Galindo <pablogsal@python.org>,
Noah Kim <noahbkim@gmail.com>,
Tim Stumbaugh <me@tjstum.com>
Discussions-To: https://discuss.python.org/t/104131
Status: Draft
Status: Accepted
Type: Standards Track
Created: 02-Oct-2025
Python-Version: 3.15
Post-History:
`03-Oct-2025 <https://discuss.python.org/t/104131>`__,
Resolution: `03-Nov-2025 <https://discuss.python.org/t/pep-810-explicit-lazy-imports/104131/466>`__


Abstract
Expand Down Expand Up @@ -361,6 +362,15 @@ being imported, and (if applicable) the fromlist. An import remains lazy only
if the filter function returns ``True``. If no lazy import filter is set, all
*potentially lazy* imports are lazy.

Note on .pth files
~~~~~~~~~~~~~~~~~~

The lazy import mechanism does not apply to .pth files processed by the
``site`` module. While .pth files have special handling for lines that begin
with ``import`` followed by a space or tab, this special handling will not be
adapted to support lazy imports. Imports specified in .pth files remain eager
as they always have been.

Lazy objects
------------

Expand Down Expand Up @@ -617,6 +627,9 @@ lazy imports filter:
``"normal"`` (respect ``lazy`` keyword only), ``"all"`` (force all imports to be
potentially lazy), or ``"none"`` (force all imports to be eager).

* ``sys.get_lazy_imports()`` - Returns the current lazy imports mode as a
string: ``"normal"``, ``"all"``, or ``"none"``.

The filter function is called for every potentially lazy import, and must
return ``True`` if the import should be lazy. This allows for fine-grained
control over which imports should be lazy, useful for excluding modules with
Expand Down Expand Up @@ -692,6 +705,11 @@ The global lazy imports flag can be controlled through:
* The ``PYTHON_LAZY_IMPORTS=<mode>`` environment variable
* The ``sys.set_lazy_imports(mode)`` function (primarily for testing)

The precedence order for setting the lazy imports mode follows the standard
Python pattern: ``sys.set_lazy_imports()`` takes highest precedence, followed
by ``-X lazy_imports=<mode>``, then ``PYTHON_LAZY_IMPORTS=<mode>``. If none
are specified, the mode defaults to ``"normal"``.

Where ``<mode>`` can be:

* ``"normal"`` (or unset): Only explicitly marked lazy imports are lazy
Expand Down