python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

Less than helpful help text for Optional[int] argument

Open genos opened this issue 4 years ago • 1 comments

Hi friends! Thanks as always for this amazing library; it's really made building CLIs in Python a breeze.

One thing I've noticed recently: when Fire-ing a function with an argument of type Optional[int], the help text for that argument has a bit of extraneous noise and gets cut off in such a way that we never see the int portion. Exempli gratia:

Environment

~/tmp ∃ python3 -m venv env
~/tmp ∃ source env/bin/activate
(env) ~/tmp ∃ pip install -U -q pip fire
(env) ~/tmp ∃ python --version
Python 3.8.7
(env) ~/tmp ∃ pip freeze
fire==0.4.0
six==1.15.0
termcolor==1.1.0

Script

#!/usr/bin/env python
from typing import Optional
from fire import Fire

def main(n: Optional[int] = None):
    n = 5 if n is None else n
    print(f"You chose {n}.")

if __name__ == "__main__":
    Fire(main)

Running

When I run ./test.py --help, I receive the following help message:

NAME
    test.py

SYNOPSIS
    test.py <flags>

FLAGS
    --n=N
        Type: Optional[typing.Unio...
        Default: None

Desiderata

Perhaps it's happening because Optional[T] is equivalent to Union[T, None], but I find the type annotation's abbreviationOptional[typing.Unio...] in the help text a little wanting; ideally I'd prefer something like Optional[int] there. Granted, if this weren't a contrived, simplified example, I'd include a docstring that described the optional integer argument and not expect fire to take care of everything for me! But it'd be nice if we could see the type parameter inside that Optional.

Thanks again!

genos avatar Mar 30 '21 13:03 genos

I'd like to do this one! I'll raise a PR for this soon.

damienjburks avatar Jan 15 '22 23:01 damienjburks