This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Matthijs Kooijman
Recipients Matthijs Kooijman, bethard, paul.j3, porton, rhettinger, zach.ware
Date 2018-08-17.21:42:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1534542139.35.0.56676864532.issue34188@psf.upfronthosting.co.za>
In-reply-to
Content
I was today trying to achieve something similar. I also considered the solution using `type` argument, but that does not seem to play well with the `choices` argument. I was going to report that as a bug, but this issue seems similar enough to comment here instead.

The solution proposed by Paul works, in the sense that if 'I' is passed on the commandline, the parsed value because `int` (the actual type, not a string, not sure if Paul really intended that). However, when running --help with his example, you get:

    usage: foo.py [-h] {<class 'float'>,<class 'int'>}

So:
  - Since the `choices` argument is used to display help text, `choices` should  contain the values that should be specified on the commandline (e.g. the *inputs* to the `type` converter.
 - Since the *type-converted* value is checked against the `choices` argument, `choices` should contain the *outputs* of the `type` converter.

AFAICS these two constraints cannot be fulfilled at the same time, except when no type conversion happens (or, when converted values can be stringified back to their unconverted value, which works in simple cases, I suppose).

IMHO this looks like a bug: `type` and `choices` do not play well together. Checking specified values against `choices` *before* type conversion happens seems more sensible to me and would fix this, as well  fullfil Victor's original usecase (though not with the syntax he suggests).
History
Date User Action Args
2018-08-17 21:42:19Matthijs Kooijmansetrecipients: + Matthijs Kooijman, rhettinger, bethard, paul.j3, zach.ware, porton
2018-08-17 21:42:19Matthijs Kooijmansetmessageid: <1534542139.35.0.56676864532.issue34188@psf.upfronthosting.co.za>
2018-08-17 21:42:19Matthijs Kooijmanlinkissue34188 messages
2018-08-17 21:42:19Matthijs Kooijmancreate