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
28 changes: 23 additions & 5 deletions peps/pep-0810.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1250,11 +1250,29 @@ method.
Why not use ``importlib.util.LazyLoader`` instead?
--------------------------------------------------

``LazyLoader`` has significant limitations:

- Requires verbose setup code for each lazy import.
- Doesn't work well with ``from ... import`` statements.
- Less clear and standard than dedicated syntax.
The standard library's :class:`~importlib.util.LazyLoader` was designed for
specific use cases but has fundamental limitations as a general-purpose lazy
import mechanism.

Most critically, ``LazyLoader`` does not support ``from ... import`` statements.
There is no straightforward mechanism to lazily import specific attributes from
a module - users would need to manually wrap and proxy individual attributes,
which is both error-prone and defeats the performance benefits.

Additionally, ``LazyLoader`` must resolve the module spec before creating the
lazy loader, which introduces overhead that reduces the performance benefits of
lazy loading. The spec resolution involves filesystem operations and path
searching that this PEP's approach defers until actual module use.

``LazyLoader`` also operates at the import machinery level rather than providing
language-level syntax, which means there's no canonical way for tools like
linters and type checkers to recognize lazy imports. A dedicated syntax enables
ecosystem-wide standardization and allows compiler and runtime optimizations
that would be impossible with a purely library-based approach.

Finally, ``LazyLoader`` requires significant boilerplate, involving manual
manipulation of module specs, loaders, and ``sys.modules``, making it impractical
for common use cases where multiple modules need to be lazily imported.

Will this break tools like ``isort`` or ``black``?
--------------------------------------------------
Expand Down