GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-156363: Speed up import of rlcompleter by deferring inspect and re by maxday · Pull Request #156364 · python/cpython

Conversation

@maxday

@maxday maxday commented Aug 25, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Speed up import of rlcompleter by deferring inspect and re
Defer the imports of inspect and re in rlcompleter into the methods that actually use them (_callable_postfix and attr_matches).

Impact
On a local build, importing rlcompleter dropped from ~16.4 ms to ~1.8 ms of cumulative import time.

Measured with:

python -X importtime -c "import rlcompleter"

Test
A regression test using test.support.import_helper.ensure_lazy_imports guards that inspect and re are not imported as a side effect of importing rlcompleter.

Fixes: #156363

  • Issue: Speed up import rlcompleter by deferring inspect and re #156363

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@maxday
pythongh-156363: Speed up import of rlcompleter by deferring inspect …
fadfaf9
…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.
@bedevere-app bedevere-app Bot mentioned this pull request Aug 25, 2026
Speed up import rlcompleter by deferring inspect and re #156363
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Aug 25, 2026
@maxday

maxday commented Aug 25, 2026

Copy link
Copy Markdown
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."

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aisk

aisk commented Aug 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
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 and maxday reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hugovk
hugovk reviewed Aug 25, 2026
View reviewed changes
Comment thread Lib/rlcompleter.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@maxday
pythongh-156363: Use PEP 810 lazy imports for inspect and re in rlcom…
0ba9e10
…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.
@maxday
maxday requested a review from hugovk August 25, 2026 17:50
StanFromIreland
StanFromIreland reviewed Aug 25, 2026
View reviewed changes
Comment thread Lib/rlcompleter.py Outdated
import __main__
import warnings
import types

@StanFromIreland StanFromIreland Aug 25, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

lazy imports shouldn't be separate.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

maxday reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
StanFromIreland
StanFromIreland reviewed Aug 25, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst Outdated
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.

@StanFromIreland StanFromIreland Aug 25, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

maxday reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@maxday
pythongh-156363: Use PEP 810 lazy imports for inspect and re in rlcom…
a79e157
…pleter

Address review feedback
@maxday

maxday commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @StanFromIreland I've addressed both of your comments :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maxday
maxday requested a review from StanFromIreland August 26, 2026 11:44
StanFromIreland
StanFromIreland reviewed Aug 26, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.RlzImp.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@python-cla-bot

python-cla-bot Bot commented Aug 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maxday @StanFromIreland
Update Misc/NEWS.d/next/Library/2026-08-25-12-43-10.gh-issue-156363.R…
fd0a08f
…lzImp.rst

Co-authored-by: Stan Ulbrych <[email protected]>
@maxday
maxday force-pushed the maxday/speedup-rlcompleter-lazy-imports branch from 95578c1 to fd0a08f Compare August 26, 2026 16:12
@aisk

aisk commented Aug 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Hi @maxday, please avoid force push in the future, see https://devguide.python.org/getting-started/pull-request-lifecycle/#don-t-force-push

And for the reviewers, I made a comment on the original issue: #156363 (comment):

One thing to notice, on the interactive startup path these two modules get imported a moment later anyway, since _pyrepl.readline is imported right after rlcompleter and pulls them both in:

$ ./python -c "import sys, _pyrepl.readline; print('re' in sys.modules, 'inspect' in sys.modules)"
True True

So the gain doesn't really materialize for the interactive interpreter. It may still help people who use this module on its own, though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maxday

maxday commented Aug 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Sorry @aisk I didn't know, I won't do it again for sure :) thanks for the link

aisk reacted with thumbs up emoji aisk reacted with laugh emoji
All reactions
  • 👍 1 reaction
  • 😄 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maxday
maxday requested a review from StanFromIreland August 27, 2026 14:54
@StanFromIreland

StanFromIreland commented Oct 22, 2026

Copy link
Copy Markdown
Member

I'm going to close, this does not seem worth it to me. If another core developer wishes to take this on, I won't block.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland StanFromIreland closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@hugovk hugovk Awaiting requested review from hugovk
@StanFromIreland StanFromIreland Awaiting requested review from StanFromIreland

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Speed up import rlcompleter by deferring inspect and re

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@maxday @aisk @StanFromIreland @hugovk
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.