click-contrib / click-repl

Subcommand REPL for click apps
MIT License
219 stars 40 forks source link

Unhandled exception in autocompletion of invalid command #108

Closed opirko-ui closed 10 months ago

opirko-ui commented 11 months ago

Hello, When I write an invalid command in Click REPL - e.g. "nonsensicalcmd" the moment I press space after the command I get this:

Unhandled exception in event loop:
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/prompt_toolkit/buffer.py", line 1919, in new_coroutine
    await coroutine(*a, **kw)
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/prompt_toolkit/buffer.py", line 1743, in async_completer
    async for completion in async_generator:
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/prompt_toolkit/completion/base.py", line 323, in get_completions_async
    async for completion in completer.get_completions_async(
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/prompt_toolkit/completion/base.py", line 199, in get_completions_async
    for item in self.get_completions(document, complete_event):
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/click_repl/_completer.py", line 257, in get_completions
    self.parsed_ctx = _resolve_context(args, self.ctx)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/click_repl/utils.py", line 46, in _resolve_context
    name, cmd, args = command.resolve_command(ctx, args)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/click/core.py", line 1746, in resolve_command
    ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))
  File "/home/abc/proj/.venv/lib/python3.11/site-packages/click/core.py", line 684, in fail
    raise UsageError(message, self)

Exception No such command 'nonsensicalcmd'.
Press ENTER to continue...

I would expect that it would write nothing up until the point when I press enter, then I'd expect something like Error: No such command 'nonsensicalcmd'. (Click without REPL writes this). It is definitely odd that there's an unhandled exception.

Used versions: click 8.1.7 click_repl 0.3.0 Python 3.11.6

opirko-ui commented 11 months ago

Try-catch with a void return here https://github.com/click-contrib/click-repl/blob/0c32dce851839fa3dd75648e503ad752c8da878a/click_repl/_completer.py#L277 seems to resolve the issue. But that's just my naive hotfix. @auvipy ?

auvipy commented 11 months ago

you can come with a draft PR with the hotfix and some sort of unit test

opirko commented 10 months ago

Very well, created https://github.com/click-contrib/click-repl/pull/109