diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -4885,6 +4885,24 @@ def test_nargs_3_metavar_length3(self): self.do_test_no_exception(nargs=3, metavar=("1", "2", "3")) +# ========================== +# add_argument choices tests +# ========================== + +class TestAddArgumentChoices(TestCase): + + def test_non_iterable_choices(self): + """Check that adding a non-iterable choices argument raises TypeError. + This also checks that it is not sufficient to support just the "in" + operator (e.g. by implementing __contains__). See issue #16468.""" + class NonIterableChoices(object): + def __contains__(self, item): + return True + parser = argparse.ArgumentParser() + self.assertRaises(TypeError, parser.add_argument, "foo", + choices=NonIterableChoices()) + + # ============================ # from argparse import * tests # ============================