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 João Eiras
Recipients João Eiras, Sworddragon, berker.peksag, bethard, ced, eric.araujo, eric.smith, macfreek, paul.j3, regis, sebix, thesociable
Date 2019-07-12.12:55:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562936153.17.0.511690868712.issue9625@roundup.psfhosted.org>
In-reply-to
Content
Another workaround for who might ever need it. The benefit of this solution comparing to a custom type is that argparse will generate the help string properly with the choices, and this solution does workaround the place when the bug happens:

    class Choices(tuple):
        # Python bug https://bugs.python.org/issue27227
        def __new__(cls, *args, **kwargs):
            x = tuple.__new__(cls, *args, **kwargs)
            Choices.__init__(x, *args, **kwargs)
            return x

        def __init__(self, *args, **kwargs):
            self.default = []

        def __contains__(self, item):
            return tuple.__contains__(self, item) or item is self.default

    choices = Choices(("value1", "value2", "value3", ...))

    parser.add_argument(
        ...,
        nargs="*",
        choices=choices,
        default=choices.default,
        ...)
History
Date User Action Args
2019-07-12 12:55:53João Eirassetrecipients: + João Eiras, bethard, macfreek, eric.smith, eric.araujo, ced, Sworddragon, thesociable, regis, berker.peksag, paul.j3, sebix
2019-07-12 12:55:53João Eirassetmessageid: <1562936153.17.0.511690868712.issue9625@roundup.psfhosted.org>
2019-07-12 12:55:53João Eiraslinkissue9625 messages
2019-07-12 12:55:52João Eirascreate