argcomplete icon indicating copy to clipboard operation
argcomplete copied to clipboard

In a REPL environment, patched parsers incorrectly assume subparsers are patched.

Open calc84maniac opened this issue 7 years ago • 2 comments

I say a REPL environment, but this really applies to any environment where an ArgumentParser is used for both parsing and completion during a single run of the program.

When parsing (not completing), newly encountered subparsers are not patched, but the parser that calls the subparser may have been patched by an earlier completion. This means that if a parsing error occurs in the subparser, the patched parser attempts to combine the subparser's _argcomplete_namespace member (which doesn't exist) with the parent namespace, causing an uncaught AttributeError.

As a workaround I could create two different ArgumentParsers, one for parsing and one for completion, but that seems a bit messy.

calc84maniac avatar Sep 25 '17 14:09 calc84maniac

Thanks for the report. Could you provide a minimal example that demonstrates the failure you're seeing?

evanunderscore avatar Sep 26 '17 02:09 evanunderscore

Sure, here's one.

import argparse
import argcomplete

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.add_parser('test').add_argument('value', choices=['valid'])

completer = argcomplete.CompletionFinder(parser)

# Complete in the main parser scope so it gets patched
i = 0
while True:
    completion = completer.rl_complete('tes', i)
    if completion is None:
        break
    print completion
    i += 1

# Error inside the unpatched subparser, throws an unhandled exception
parser.parse_args(['test', 'invalid'])

calc84maniac avatar Sep 26 '17 14:09 calc84maniac