larryhastings / appeal

Command-line parsing library for Python 3.
Other
128 stars 7 forks source link

Type annotation of "bool" causes error producing usage docs #15

Closed bennuttall closed 1 year ago

bennuttall commented 1 year ago

This example doesn't use type hints:

@app.command()
def foo(name, *, verbose = False):
    print(f"Hello, {name}!")
    if verbose:
        print("Here, have some verbosity")

And the output of help works ok:

usage: main.py command

Commands:

    foo
    help            Print usage documentation on a specific command.

Adding a type hint of bool like so:

@app.command()
def foo(name: str, *, verbose: bool = False):
    print(f"Hello, {name}!")
    if verbose:
        print("Here, have some verbosity")

causes a traceback.

larryhastings commented 1 year ago

I have a big patch in development right now fixing usage. I'll throw this on the heap of things I'm doing.

I assert that the behavior you want here is for an explicit annotation of bool to behave identically to a default value of True or False. (Which means internally we replace it with BooleanOptionConverter.)

p.s. this is a bug, a quick gander at the code suggests it's supposed to already work that way.

larryhastings commented 1 year ago

p.p.s. one rarely uses type hints with Appeal, beyond typing.Annotated or perhaps typing.no_type_check(). However Appeal does make heavy use of annotations.

bennuttall commented 1 year ago

Ah, yes.