|
8 | 8 | |
9 | 9 | |
10 | 10 | Discussions-To: https://discuss.python.org/t/104131 |
11 | | -Status: Draft |
| 11 | +Status: Accepted |
12 | 12 | Type: Standards Track |
13 | 13 | Created: 02-Oct-2025 |
14 | 14 | Python-Version: 3.15 |
15 | 15 | Post-History: |
16 | 16 | `03-Oct-2025 <https://discuss.python.org/t/104131>`__, |
| 17 | +Resolution: `03-Nov-2025 <https://discuss.python.org/t/pep-810-explicit-lazy-imports/104131/466>`__ |
17 | 18 |
|
18 | 19 |
|
19 | 20 | Abstract |
@@ -361,6 +362,15 @@ being imported, and (if applicable) the fromlist. An import remains lazy only |
361 | 362 | if the filter function returns ``True``. If no lazy import filter is set, all |
362 | 363 | *potentially lazy* imports are lazy. |
363 | 364 |
|
| 365 | +Note on .pth files |
| 366 | +~~~~~~~~~~~~~~~~~~ |
| 367 | + |
| 368 | +The lazy import mechanism does not apply to .pth files processed by the |
| 369 | +``site`` module. While .pth files have special handling for lines that begin |
| 370 | +with ``import`` followed by a space or tab, this special handling will not be |
| 371 | +adapted to support lazy imports. Imports specified in .pth files remain eager |
| 372 | +as they always have been. |
| 373 | + |
364 | 374 | Lazy objects |
365 | 375 | ------------ |
366 | 376 |
|
@@ -617,6 +627,9 @@ lazy imports filter: |
617 | 627 | ``"normal"`` (respect ``lazy`` keyword only), ``"all"`` (force all imports to be |
618 | 628 | potentially lazy), or ``"none"`` (force all imports to be eager). |
619 | 629 |
|
| 630 | +* ``sys.get_lazy_imports()`` - Returns the current lazy imports mode as a |
| 631 | + string: ``"normal"``, ``"all"``, or ``"none"``. |
| 632 | + |
620 | 633 | The filter function is called for every potentially lazy import, and must |
621 | 634 | return ``True`` if the import should be lazy. This allows for fine-grained |
622 | 635 | control over which imports should be lazy, useful for excluding modules with |
@@ -692,6 +705,11 @@ The global lazy imports flag can be controlled through: |
692 | 705 | * The ``PYTHON_LAZY_IMPORTS=<mode>`` environment variable |
693 | 706 | * The ``sys.set_lazy_imports(mode)`` function (primarily for testing) |
694 | 707 |
|
| 708 | +The precedence order for setting the lazy imports mode follows the standard |
| 709 | +Python pattern: ``sys.set_lazy_imports()`` takes highest precedence, followed |
| 710 | +by ``-X lazy_imports=<mode>``, then ``PYTHON_LAZY_IMPORTS=<mode>``. If none |
| 711 | +are specified, the mode defaults to ``"normal"``. |
| 712 | + |
695 | 713 | Where ``<mode>`` can be: |
696 | 714 |
|
697 | 715 | * ``"normal"`` (or unset): Only explicitly marked lazy imports are lazy |
|
0 commit comments