Enchancement suggestion for argparse: intuit type from default

Robert Kern robert.kern at gmail.com
Thu Mar 15 11:06:48 CET 2012

On 3/15/12 5:59 AM, Cameron Simpson wrote:
> On 15Mar2012 12:22, Ben Finney<ben+python at benfinney.id.au>  wrote:
> | Roy Smith<roy at panix.com>  writes:
> |>  I'll admit I hadn't considered that, but I don't see it as a major
> |>  problem. The type intuition could be designed to only work for types
> |>  other than NoneType.
> |
> | −1, then. It's growing too many special cases, and is no longer simple
> | to describe, so that indicates it's probably a bad idea.
> If `type` is not supplied and `default` is present and not None, `type`
> shall be the type of `default`.
> That seems straightforward to me. It's a single sentence, easy to read
> and understand, and potentially saves a lot of code verbiage (gratuitous
> type= prarameters). I say "gratuitous" because unless `default` is a
> sentinel for "no option supplied", the `type` should always match
> type(default). Or am I wrong about that?

Yes. Not all type(default) types can be called with a string to produce a valid 
value. Note that "type=" is really a misnomer. argparse doesn't really want a 
type object there; it wants a converter function that takes a string to an object.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list