Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

argparse: parse_args with a subparser + namespace tries to setdefault on a mappingproxy object #116850

Closed
Assignees
serhiy-storchaka
Labels
3.12only security fixes3.13bugs and security fixes3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error
@alex-tianhuang

Description

@alex-tianhuang

Bug report

Bug description:

I was using the namespace argument in parse_args and found that argparse tries to setdefault on a mapping proxy when parsing unknown extra attributes:

importargparseparser=argparse.ArgumentParser()subparser=argparse.ArgumentParser()subparsers=parser.add_subparsers()subparsers.add_parser(name="sub",add_help=False)classMyNamespace:passtry:parser.parse_args(["sub","oops"],namespace=MyNamespace)exceptSystemExit:pass
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_argsargs, argv = self.parse_known_args(args, namespace)File "<python_installation>/lib/python3.12/argparse.py", line 1924, in parse_known_argsnamespace, args = self._parse_known_args(args, namespace)File "<python_installation>/lib/python3.12/argparse.py", line 2139, in _parse_known_argsstop_index = consume_positionals(start_index)File "<python_installation>/lib/python3.12/argparse.py", line 2095, in consume_positionalstake_action(action, args)File "<python_installation>/lib/python3.12/argparse.py", line 2000, in take_actionaction(self, namespace, argument_values, option_string)File "<python_installation>/lib/python3.12/argparse.py", line 1268, in callvars(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.

Using python 3.12.1 / argparse 1.1 / osx-arm64.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Labels

3.12only security fixes3.13bugs and security fixes3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error

Projects

Status

Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp