gh-156363: Speed up import of rlcompleter by deferring inspect and re - #156364
Open
maxday wants to merge 3 commits into
Open
gh-156363: Speed up import of rlcompleter by deferring inspect and re#156364maxday wants to merge 3 commits into
maxday wants to merge 3 commits into
Conversation
…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.
Contributor
Author
|
I believe the macOS failing test can be rerun, looks like it's a network error issue, from the log: "The hosted runner lost communication with the server. Anything in your workflow that terminates the runner process, starves it for CPU/Memory, or blocks its network access can cause this error." |
Member
|
If we decide to do this, we can use the lazy import syntax introduced in https://peps.python.org/pep-0810/ to resolve it. |
hugovk
reviewed
Aug 25, 2026
…pleter 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.
| import __main__ | ||
| import warnings | ||
| import types | ||
|
|
Member
There was a problem hiding this comment.
Suggested change
lazy imports shouldn't be separate.
Comment on lines
+1
to
+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. |
Member
There was a problem hiding this comment.
Suggested change
| 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. |
The rest is implementation details.
…pleter Address review feedback
Contributor
Author
|
Thanks for the review @StanFromIreland I've addressed both of your comments :) |
| @@ -0,0 +1 @@ | |||
| Speed up the :mod:`rlcompleter` module's import time. No newline at end of file | |||
Member
There was a problem hiding this comment.
Suggested change
| Speed up the :mod:`rlcompleter` module's import time. | |
| Speed up the :mod:`rlcompleter` module's import time. | |
Missing newline for lint.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Speed up import of rlcompleter by deferring
inspectandreDefer the imports of
inspectandreinrlcompleterinto the methods that actually use them (_callable_postfixandattr_matches).Impact
On a local build, importing
rlcompleterdropped from ~16.4 ms to ~1.8 ms of cumulative import time.Measured with:
Test
A regression test using
test.support.import_helper.ensure_lazy_importsguards thatinspectandreare not imported as a side effect of importingrlcompleter.Fixes: #156363