[3.12] gh-116850: Fix argparse for namespaces with not directly writable dict (GH-124667) (GH-124758)

It now always uses setattr() instead of setting the dict item to modify
the namespace. This allows to use a class as a namespace.
(cherry picked from commit 95e92ef6c7)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-09-29 10:40:00 +02:00 committed by GitHub
parent 7677be5ee6
commit 5464c8aa98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 1 deletions

View file

@ -1269,7 +1269,8 @@ class _SubParsersAction(Action):
setattr(namespace, key, value)
if arg_strings:
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
if not hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
setattr(namespace, _UNRECOGNIZED_ARGS_ATTR, [])
getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
class _ExtendAction(_AppendAction):