summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-09-29 07:57:21 (GMT)
committerGitHub <noreply@github.com>2024-09-29 07:57:21 (GMT)
commitf1a2417b9e2993e584610851ac004c8b0599b323 (patch)
tree5b6aec01fa50ef586c97d701a9ad4be743a170e0 /Lib/argparse.py
parentdac4ec52866e4068f3ac33b4da1e1a1fe6fc2cba (diff)
downloadcpython-f1a2417b9e2993e584610851ac004c8b0599b323.zip
cpython-f1a2417b9e2993e584610851ac004c8b0599b323.tar.gz
cpython-f1a2417b9e2993e584610851ac004c8b0599b323.tar.bz2
gh-61181: Fix support of choices with string value in argparse (GH-124578)
Substrings of the specified string no longer considered valid values.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index f428512..a671c8b 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2552,11 +2552,15 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
- if action.choices is not None and value not in action.choices:
- args = {'value': value,
- 'choices': ', '.join(map(repr, action.choices))}
- msg = _('invalid choice: %(value)r (choose from %(choices)s)')
- raise ArgumentError(action, msg % args)
+ choices = action.choices
+ if choices is not None:
+ if isinstance(choices, str):
+ choices = iter(choices)
+ if value not in choices:
+ args = {'value': value,
+ 'choices': ', '.join(map(repr, action.choices))}
+ msg = _('invalid choice: %(value)r (choose from %(choices)s)')
+ raise ArgumentError(action, msg % args)
# =======================
# Help-formatting methods