From fadfaf918c300ecba5a31fb98de63ac5c310a1f6 Mon Sep 17 00:00:00 2001 From: Maxime David Date: Tue, 25 Aug 2026 12:43:41 +0000 Subject: [PATCH 1/4] gh-156363: Speed up import of rlcompleter by deferring inspect and re rlcompleter imported inspect and re at module scope, but each is used in exactly one completion method (inspect in Completer._callable_postfix, re in Completer.attr_matches). Neither is needed to construct a Completer or set up interactive completion, only to compute completions. inspect in particular is a heavy import (it pulls in dis, tokenize, ...), so importing rlcompleter dropped from ~16.4ms to ~1.8ms of cumulative import time on a local build. Defer both imports into the methods that use them and add a lazy-import guard test. --- Lib/rlcompleter.py | 4 ++-- Lib/test/test_rlcompleter.py | 5 +++++ .../Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py index 26fcda612567ea4..cda87d659739cef 100644 --- a/Lib/rlcompleter.py +++ b/Lib/rlcompleter.py @@ -31,9 +31,7 @@ import atexit import builtins -import inspect import keyword -import re import __main__ import warnings import types @@ -105,6 +103,7 @@ def complete(self, text, state): def _callable_postfix(self, val, word): if callable(val): + import inspect word += "(" try: if not inspect.signature(val).parameters: @@ -153,6 +152,7 @@ def attr_matches(self, text): with a __getattr__ hook is evaluated. """ + import re m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return [] diff --git a/Lib/test/test_rlcompleter.py b/Lib/test/test_rlcompleter.py index c0b5a4da8cb2569..07a1e53212c3110 100644 --- a/Lib/test/test_rlcompleter.py +++ b/Lib/test/test_rlcompleter.py @@ -4,6 +4,7 @@ import types import rlcompleter from test.support import MISSING_C_DOCSTRINGS +from test.support.import_helper import ensure_lazy_imports class CompleteMe: """ Trivial class used in testing rlcompleter.Completer. """ @@ -252,5 +253,9 @@ def test_duplicate_globals(self): self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis()') self.assertIsNone(completer.complete('Ellipsis', 1)) + def test_lazy_imports(self): + ensure_lazy_imports("rlcompleter", {"inspect", "re"}) + + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst b/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst new file mode 100644 index 000000000000000..e11bab17059cbbf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst @@ -0,0 +1,5 @@ +Speed up ``import rlcompleter`` by deferring the imports of :mod:`inspect` +and :mod:`re` into the completion methods that use them. They are only +needed while computing completions, so importing :mod:`rlcompleter` (for +example when setting up interactive completion) no longer pays their import +cost. From 0ba9e101ee6b3b9df9a19ab536a72b41349b00a1 Mon Sep 17 00:00:00 2001 From: Maxime David Date: Tue, 25 Aug 2026 17:28:56 +0000 Subject: [PATCH 2/4] gh-156363: Use PEP 810 lazy imports for inspect and re in rlcompleter Address review feedback: replace deferred in-function imports of `inspect` and `re` with `lazy import` statements at the top of the module, keeping them alongside the regular imports. --- Lib/rlcompleter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py index cda87d659739cef..2f04e2c0e458c86 100644 --- a/Lib/rlcompleter.py +++ b/Lib/rlcompleter.py @@ -36,6 +36,9 @@ import warnings import types +lazy import inspect +lazy import re + __all__ = ["Completer"] # Sentinel object to distinguish "missing" from "present but None" @@ -103,7 +106,6 @@ def complete(self, text, state): def _callable_postfix(self, val, word): if callable(val): - import inspect word += "(" try: if not inspect.signature(val).parameters: @@ -152,7 +154,6 @@ def attr_matches(self, text): with a __getattr__ hook is evaluated. """ - import re m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return [] From a79e15795b5cfcdb53f2d3203582055aead98561 Mon Sep 17 00:00:00 2001 From: Maxime David Date: Wed, 26 Aug 2026 11:42:25 +0000 Subject: [PATCH 3/4] gh-156363: Use PEP 810 lazy imports for inspect and re in rlcompleter Address review feedback --- Lib/rlcompleter.py | 1 - .../Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py index 2f04e2c0e458c86..542da206e2c6920 100644 --- a/Lib/rlcompleter.py +++ b/Lib/rlcompleter.py @@ -35,7 +35,6 @@ import __main__ import warnings import types - lazy import inspect lazy import re diff --git a/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst b/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst index e11bab17059cbbf..38192474acce21b 100644 --- a/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst +++ b/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst @@ -1,5 +1 @@ -Speed up ``import rlcompleter`` by deferring the imports of :mod:`inspect` -and :mod:`re` into the completion methods that use them. They are only -needed while computing completions, so importing :mod:`rlcompleter` (for -example when setting up interactive completion) no longer pays their import -cost. +Speed up the :mod:`rlcompleter` module's import time. \ No newline at end of file From fd0a08f22d9686065afa3a1dd9afcb2b29310cb1 Mon Sep 17 00:00:00 2001 From: Maxime David Date: Wed, 26 Aug 2026 18:08:14 +0200 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst Co-authored-by: Stan Ulbrych --- .../next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst b/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst index 38192474acce21b..993ea7d839ed502 100644 --- a/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst +++ b/Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst @@ -1 +1 @@ -Speed up the :mod:`rlcompleter` module's import time. \ No newline at end of file +Speed up the :mod:`rlcompleter` module's import time.