Skip to content

Commit 09815f2

Browse files
authored
PEP 810: Add clarification on the __lazy_modules__ behavior (#4674)
1 parent a5274ce commit 09815f2

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

peps/pep-0810.rst

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,16 @@ Example with ``lazy from ... import``:
324324
# Accessing 'loads' now reifies it (json already loaded, no re-import)
325325
data = loads(result)
326326
327-
A module may contain a :data:`!__lazy_modules__` attribute, which is a
328-
sequence of fully qualified module names (strings) to make *potentially lazy*
329-
(as if the ``lazy`` keyword was used). This attribute is checked on each
330-
``import`` statement to determine whether the import should be made
331-
*potentially lazy*. When a module is made lazy this way, from-imports using
332-
that module are also lazy, but not necessarily imports of sub-modules.
327+
A module may define a :data:`!__lazy_modules__` variable in its global scope,
328+
which specifies which module names should be made *potentially lazy* (as if the
329+
``lazy`` keyword was used). This variable is checked on each ``import``
330+
statement to determine whether the import should be made *potentially lazy*.
331+
The check is performed by calling ``__contains__`` on the
332+
:data:`!__lazy_modules__` object with a string containing the fully qualified
333+
module name being imported. Typically, :data:`!__lazy_modules__` is a set of
334+
fully qualified module name strings. When a module is made lazy this way,
335+
from-imports using that module are also lazy, but not necessarily imports of
336+
sub-modules.
333337

334338
The normal (non-lazy) import statement will check the global lazy imports
335339
flag. If it is "all", all imports are *potentially lazy* (except for
@@ -1636,6 +1640,28 @@ From the discussion on :pep:`690` it is clear that this is a fairly
16361640
contentious idea, although perhaps once we have wide-spread use of lazy
16371641
imports this can be reconsidered.
16381642

1643+
Supporting ``__lazy_modules__ = ["*"]`` as built-in syntax
1644+
-----------------------------------------------------------
1645+
1646+
The suggestion to support ``__lazy_modules__ = ["*"]`` as a convenient way to
1647+
make all imports in a module lazy without explicit enumeration has been
1648+
considered. This approach was rejected because :data:`!__lazy_modules__` already
1649+
represents implicit action-at-a-distance behavior that is tolerated solely as a
1650+
backwards compatibility mechanism. Extending support to wildcard patterns would
1651+
significantly increase implementation complexity and invite scope creep into
1652+
pattern matching and globbing functionality. As :data:`!__lazy_modules__` is a
1653+
permanent language feature that cannot be removed in future versions, the design
1654+
prioritizes minimalism and restricts its scope to serving as a transitional tool
1655+
for backwards compatibility.
1656+
1657+
It is worth noting that the implementation performs membership checks by calling
1658+
``__contains__`` on the :data:`!__lazy_modules__` object. Consequently, users
1659+
requiring wildcard behavior may provide a custom object implementing
1660+
``__contains__`` to return ``True`` for all queries or other desired patterns.
1661+
This design provides the necessary flexibility for advanced use cases while
1662+
maintaining a simple, focused specification for the primary mechanism. If this PEP is accepted, adding
1663+
such helper objects to the standard library can be discussed in a future issue. Presently, it is out of scope for this PEP.
1664+
16391665
Disallowing lazy imports inside ``with`` blocks
16401666
------------------------------------------------
16411667

0 commit comments

Comments
 (0)