diff --git a/peps/pep-0810.rst b/peps/pep-0810.rst index 58a635f641d..c2b7e2a2e39 100644 --- a/peps/pep-0810.rst +++ b/peps/pep-0810.rst @@ -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``? --------------------------------------------------