Enchancement suggestion for argparse: intuit type from default

Ian Kelly ian.g.kelly at gmail.com
Wed Mar 14 20:03:28 CET 2012


On Wed, Mar 14, 2012 at 7:30 AM, Roy Smith <roy at panix.com> wrote:
> It's already inferred that the type is a string if you don't give it any
> value.  What possible meaning could:
>
> parser.add_argument('--foo', default=100)
>
> have?  If I run the program with:
>
> $ prog
>
> then foo defaults to the integer 100, but if I run it with:
>
> $ prog --foo=100
>
> then I get the string "100"?  Surely there's not much of a use case for
> that.

What about:

parser.add_argument('--foo', default=None)

Probably it should not infer NoneType as the argument type in this
case.  So would it just ignore the default in this case and let the
type remain str?

Also, how would the inference interact with different actions?  For example:

parser.add_argument('--foo', action='append', default=['one'])

I'm not exactly sure what a use case for this might be, but anyway,
the type here should clearly be str, not list.  And then what about
this variation:

parser.add_argument('--foo', action='append', default=[1])

Should it try to infer that because the list contains an int, the type
should be int?  And even if you manage to get the inference working
flawlessly and expectedly for append, what about custom actions?

It seems to me that there are a large number of edge cases here that
will end up hurting predictability for the end user.

Cheers,
Ian



More information about the Python-list mailing list