Skip to content

Commit 4fcdf85

Browse files
authored
PEP 810: Add some clarifications on LazyLoader (#4677)
1 parent 2554992 commit 4fcdf85

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

peps/pep-0810.rst

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,11 +1250,29 @@ method.
12501250
Why not use ``importlib.util.LazyLoader`` instead?
12511251
--------------------------------------------------
12521252

1253-
``LazyLoader`` has significant limitations:
1254-
1255-
- Requires verbose setup code for each lazy import.
1256-
- Doesn't work well with ``from ... import`` statements.
1257-
- Less clear and standard than dedicated syntax.
1253+
The standard library's :class:`~importlib.util.LazyLoader` was designed for
1254+
specific use cases but has fundamental limitations as a general-purpose lazy
1255+
import mechanism.
1256+
1257+
Most critically, ``LazyLoader`` does not support ``from ... import`` statements.
1258+
There is no straightforward mechanism to lazily import specific attributes from
1259+
a module - users would need to manually wrap and proxy individual attributes,
1260+
which is both error-prone and defeats the performance benefits.
1261+
1262+
Additionally, ``LazyLoader`` must resolve the module spec before creating the
1263+
lazy loader, which introduces overhead that reduces the performance benefits of
1264+
lazy loading. The spec resolution involves filesystem operations and path
1265+
searching that this PEP's approach defers until actual module use.
1266+
1267+
``LazyLoader`` also operates at the import machinery level rather than providing
1268+
language-level syntax, which means there's no canonical way for tools like
1269+
linters and type checkers to recognize lazy imports. A dedicated syntax enables
1270+
ecosystem-wide standardization and allows compiler and runtime optimizations
1271+
that would be impossible with a purely library-based approach.
1272+
1273+
Finally, ``LazyLoader`` requires significant boilerplate, involving manual
1274+
manipulation of module specs, loaders, and ``sys.modules``, making it impractical
1275+
for common use cases where multiple modules need to be lazily imported.
12581276

12591277
Will this break tools like ``isort`` or ``black``?
12601278
--------------------------------------------------

0 commit comments

Comments
 (0)