From 34b73dfddd07a1d987079b292b64be8ad45d7408 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 25 Oct 2025 18:43:20 +0100 Subject: [PATCH 1/3] PEP 810: Add clarification on the __lazy_modules__ behavior --- peps/pep-0810.rst | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/peps/pep-0810.rst b/peps/pep-0810.rst index 418bd692570..c7f522fa79d 100644 --- a/peps/pep-0810.rst +++ b/peps/pep-0810.rst @@ -324,12 +324,16 @@ Example with ``lazy from ... import``: # Accessing 'loads' now reifies it (json already loaded, no re-import) data = loads(result) -A module may contain a :data:`!__lazy_modules__` attribute, which is a -sequence of fully qualified module names (strings) to make *potentially lazy* -(as if the ``lazy`` keyword was used). This attribute is checked on each -``import`` statement to determine whether the import should be made -*potentially lazy*. When a module is made lazy this way, from-imports using -that module are also lazy, but not necessarily imports of sub-modules. +A module may define a :data:`!__lazy_modules__` variable in its global scope, +which specifies which module names should be made *potentially lazy* (as if the +``lazy`` keyword was used). This variable is checked on each ``import`` +statement to determine whether the import should be made *potentially lazy*. +The check is performed by calling ``__contains__`` on the +:data:`!__lazy_modules__` object with a string containing the fully qualified +module name being imported. Typically, :data:`!__lazy_modules__` is a list of +fully qualified module name strings. When a module is made lazy this way, +from-imports using that module are also lazy, but not necessarily imports of +sub-modules. The normal (non-lazy) import statement will check the global lazy imports flag. If it is "all", all imports are *potentially lazy* (except for @@ -1636,6 +1640,29 @@ From the discussion on :pep:`690` it is clear that this is a fairly contentious idea, although perhaps once we have wide-spread use of lazy imports this can be reconsidered. +Supporting ``__lazy_modules__ = ["*"]`` as built-in syntax +----------------------------------------------------------- + +The suggestion to support ``__lazy_modules__ = ["*"]`` as a convenient way to +make all imports in a module lazy without explicit enumeration has been +considered. This approach was rejected because :data:`!__lazy_modules__` already +represents implicit action-at-a-distance behavior that is tolerated solely as a +backwards compatibility mechanism. Extending support to wildcard patterns would +significantly increase implementation complexity and invite scope creep into +pattern matching and globbing functionality. As :data:`!__lazy_modules__` is a +permanent language feature that cannot be removed in future versions, the design +prioritizes minimalism and restricts its scope to serving as a transitional tool +for backwards compatibility. + +It is worth noting that the implementation performs membership checks by calling +``__contains__`` on the :data:`!__lazy_modules__` object. Consequently, users +requiring wildcard behavior may provide a custom object implementing +``__contains__`` to return ``True`` for all queries or other desired patterns. +This design provides the necessary flexibility for advanced use cases while +maintaining a simple and focused specification for the primary mechanism. Adding +such helper objects to the standard library can be discussed in a future issue +if this PEP is accepted. + Disallowing lazy imports inside ``with`` blocks ------------------------------------------------ From fca79527a5add5dd5f24d33fd1365a8b8406c708 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 25 Oct 2025 19:24:06 +0100 Subject: [PATCH 2/3] Update peps/pep-0810.rst --- peps/pep-0810.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0810.rst b/peps/pep-0810.rst index c7f522fa79d..6682daaff15 100644 --- a/peps/pep-0810.rst +++ b/peps/pep-0810.rst @@ -330,7 +330,7 @@ which specifies which module names should be made *potentially lazy* (as if the statement to determine whether the import should be made *potentially lazy*. The check is performed by calling ``__contains__`` on the :data:`!__lazy_modules__` object with a string containing the fully qualified -module name being imported. Typically, :data:`!__lazy_modules__` is a list of +module name being imported. Typically, :data:`!__lazy_modules__` is a set of fully qualified module name strings. When a module is made lazy this way, from-imports using that module are also lazy, but not necessarily imports of sub-modules. From aef114e54429ab60542b35cc98b69972c487011f Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 25 Oct 2025 19:24:47 +0100 Subject: [PATCH 3/3] Update peps/pep-0810.rst Co-authored-by: Carol Willing --- peps/pep-0810.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/peps/pep-0810.rst b/peps/pep-0810.rst index 6682daaff15..6d92b3d2ff0 100644 --- a/peps/pep-0810.rst +++ b/peps/pep-0810.rst @@ -1659,9 +1659,8 @@ It is worth noting that the implementation performs membership checks by calling requiring wildcard behavior may provide a custom object implementing ``__contains__`` to return ``True`` for all queries or other desired patterns. This design provides the necessary flexibility for advanced use cases while -maintaining a simple and focused specification for the primary mechanism. Adding -such helper objects to the standard library can be discussed in a future issue -if this PEP is accepted. +maintaining a simple, focused specification for the primary mechanism. If this PEP is accepted, adding +such helper objects to the standard library can be discussed in a future issue. Presently, it is out of scope for this PEP. Disallowing lazy imports inside ``with`` blocks ------------------------------------------------