Skip to content

Conversation

@loic-simon
Copy link
Contributor

@loic-simon loic-simon commented Nov 1, 2025

Add the ability for _pyrepl._module_completer.ModuleCompleter to suggest attributes, in addition to modules.

If the module to import from is not imported yet, ask the user if they want to import it.
This is done by adding the notion of completion action to _pyrepl.completing_reader.CompletingReader:

  • get_completions can return a completion action, in addtion to the suggestions list
  • A completion action is a prompt to show + a callback to call if the user press TAB again
  • The callback can return a message to show instead (here, if import failed)
  • After the callback is fired, get_completions is called again, so it can propose new results (here, module attributes)
  • If the user does any other input than TAB when the prompt is shown, the action is discarded

Other changes:

  • I had to tweak a little the code used to display a message below the prompt, to handle the case when it doesn't fit on the console
  • Some unrelated tests to get the coverage rate of _module_completer to 100% (in a separate commit, I can remove it if needed)

Interactive WebAssembly demo (up to date): https://pyrepl-attributes-import-completion.pages.dev

Possible improvement (probably in a follow-up PR): make modules printing stuff when imported not mess with the prompt ; see this Discourse message

@loic-simon loic-simon changed the title gh-14870: PyREPL auto-complete module attributes in import statements gh-140870: PyREPL auto-complete module attributes in import statements Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant