]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-150579: use lazy imports for concurrent.futures (GH-150585)
authorHenry Schreiner <henryfs@princeton.edu>
Fri, 3 Jul 2026 18:11:16 +0000 (14:11 -0400)
committerGitHub <noreply@github.com>
Fri, 3 Jul 2026 18:11:16 +0000 (18:11 +0000)
This module has a manual lazy import hack using `__getattr__`. Now that lazy imports exist and cannot be disabled, this could use lazy imports instead.

Key differences: this will now show up in sys.lazy_modules when accessed. Error messages should be a bit better without the wrapper `__getattr__` involved.  That's the only differences I can think of.

Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/concurrent/futures/__init__.py
Misc/NEWS.d/next/Library/2026-07-03-17-29-34.gh-issue-150579.0tLpQukIU.rst [new file with mode: 0644]

index d6ac4b3e0b675f60cfdaa949ddba883a160298c8..5d53a5ac50931d3555b980c414174f8880bc7db7 100644 (file)
@@ -17,6 +17,9 @@ from concurrent.futures._base import (FIRST_COMPLETED,
                                       wait,
                                       as_completed)
 
+lazy from .process import ProcessPoolExecutor
+lazy from .thread import ThreadPoolExecutor
+
 __all__ = [
     'FIRST_COMPLETED',
     'FIRST_EXCEPTION',
@@ -40,26 +43,9 @@ except ImportError:
     _interpreters = None
 
 if _interpreters:
+    lazy from .interpreter import InterpreterPoolExecutor  # noqa: F401
     __all__.append('InterpreterPoolExecutor')
 
 
 def __dir__():
     return __all__ + ['__author__', '__doc__']
-
-
-def __getattr__(name):
-    global ProcessPoolExecutor, ThreadPoolExecutor, InterpreterPoolExecutor
-
-    if name == 'ProcessPoolExecutor':
-        from .process import ProcessPoolExecutor
-        return ProcessPoolExecutor
-
-    if name == 'ThreadPoolExecutor':
-        from .thread import ThreadPoolExecutor
-        return ThreadPoolExecutor
-
-    if _interpreters and name == 'InterpreterPoolExecutor':
-        from .interpreter import InterpreterPoolExecutor
-        return InterpreterPoolExecutor
-
-    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/Misc/NEWS.d/next/Library/2026-07-03-17-29-34.gh-issue-150579.0tLpQukIU.rst b/Misc/NEWS.d/next/Library/2026-07-03-17-29-34.gh-issue-150579.0tLpQukIU.rst
new file mode 100644 (file)
index 0000000..7aff384
--- /dev/null
@@ -0,0 +1,2 @@
+:mod:`concurrent.futures` now uses lazy imports for its executor submodules
+instead of a module ``__getattr__`` hook.