-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
argparse: parse_args with a subparser + namespace tries to setdefault on a mappingproxy object #116850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, I had same issue. I think you have to create instance of your namespace class. class MyNamespace:
pass
try:
my_namespace = MyNamespace() <---------------------------------------------
parser.parse_args(["sub", "oops"], namespace=my_namespace)
except SystemExit:
pass https://docs.python.org/3/library/argparse.html#argparse.Namespace |
serhiy-storchaka
added a commit
to serhiy-storchaka/cpython
that referenced
this issue
Sep 27, 2024
…le dict It now always uses setattr() instead of setting the dict item to modify the namespace. This allows to use a class as a namespace.
serhiy-storchaka
added a commit
that referenced
this issue
Sep 29, 2024
GH-124667) It now always uses setattr() instead of setting the dict item to modify the namespace. This allows to use a class as a namespace.
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Sep 29, 2024
…le dict (pythonGH-124667) 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 95e92ef) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Sep 29, 2024
…le dict (pythonGH-124667) 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 95e92ef) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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
The text was updated successfully, but these errors were encountered: