◐ Shell
clean mode source ↗

gh-69605: PyREPL: insert "import" after "from foo <tab>" by loic-simon · Pull Request #148445 · python/cpython

Two import completion improvements:

Input Current Proposed
from math <tab> from math from math import
from mat<tab> from math from math (extra space)

The first one should be pretty uncontroversial, since import is the only valid syntax here.

The second only change the behavior when a single match is found:

  • from cont<tab> still inserts from context (common prefix to contextlib and contextvar)
  • from foo<tab> still inserts nothing, since we have no match

It allows from mat<tab><tab> to insert from math import , which I find really smooth and pleasant to use!


Implementation notes: I settled to extend ImportParser.parse to also return a space_end boolean, and use that in ModuleCompleter.complete to return an import suggestion / add a space when needed.

This is a little more generic that needed for this change (eg. we don't really care abuout the space in import foo ), but it keep the parsing logic and completion logic orthogonal.

cc @tomasr8 (it'll have a few conflicts with your coloring PR, but I have no way to point this PR to some virtual "after your PR is merged" branch, have I?)