You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse: parse_args with a subparser + namespace tries to setdefault on a mappingproxy object #116850
Traceback (most recent call last):
File "<path_to_script>", line 16, in <module>
parser.parse_args(["sub", "oops"], namespace=MyNamespace)
File "<python_installation>/lib/python3.12/argparse.py", line 1891, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "<python_installation>/lib/python3.12/argparse.py", line 1924, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "<python_installation>/lib/python3.12/argparse.py", line 2139, in _parse_known_args
stop_index = consume_positionals(start_index)
File "<python_installation>/lib/python3.12/argparse.py", line 2095, in consume_positionals
take_action(action, args)
File "<python_installation>/lib/python3.12/argparse.py", line 2000, in take_action
action(self, namespace, argument_values, option_string)
File "<python_installation>/lib/python3.12/argparse.py", line 1268, in call
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
AttributeError: 'mappingproxy' object has no attribute 'setdefault'
I suspect this is because we're attempting to do vars(MyNamespace) instead of vars(argparse.Namespace) but I don't know much more than that.
I skimmed the top couple entries on the issue tracker with keywords 'argparse mappingproxy / argparse namespace' and found nothing, so I've decided that maybe nobody has reported this yet.
Activity
mimros commentedon May 14, 2024
Hi, I had same issue. I think you have to create instance of your namespace class.
https://docs.python.org/3/library/argparse.html#argparse.Namespace
pythongh-116850: Fix argparse for namespaces with not directly writab…
gh-116850: Fix argparse for namespaces with not directly writable dict (
pythongh-116850: Fix argparse for namespaces with not directly writab…
pythongh-116850: Fix argparse for namespaces with not directly writab…
[3.12] gh-116850: Fix argparse for namespaces with not directly writa…
[3.13] gh-116850: Fix argparse for namespaces with not directly writa…