- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
Issue41684
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2020-09-01 08:33 by lucca.ruhland, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Messages (5) | |||
|---|---|---|---|
| msg376183 - (view) | Author: Lucca Ruhland (lucca.ruhland) | Date: 2020-09-01 08:33 | |
When parsing arguments with a namespace object, the subparser are behaving different than the main parser, although this is not stated in the documentation.
Each attribute which is not already part of the namespace, should be saved into the namespace object.
Therefore any already existing namespace attribute should overwrite the default value of any argument which is not explicitly set.
While this is true for the parent parser, it does not work for the subparser.
Here is a small example code:
------------------------------------------------------------------------
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--root', type=str, default='.')
subparsers = parser.add_subparsers()
subparser = subparsers.add_parser('subp')
subparser.add_argument('--subroot', type=str, default='./subdir')
our_args = argparse.Namespace(root="./config_root", subroot="./config_subdir")
argv = ['subp']
args = parser.parse_args(argv, namespace=our_args)
print(args)
------------------------------------------------------------------------
>>> Expected: Namespace(root='./config_root', subroot='./config_subdir')
>>> Output: Namespace(root='./config_root', subroot='./subdir')
When calling the subparser, the namespace attribute is overwritten by the default value.
|
|||
| msg376194 - (view) | Author: paul j3 (paul.j3) * ![]() |
Date: 2020-09-01 15:42 | |
I've noted this behavior before. https://bugs.python.org/issue27859 argparse - subparsers does not retain namespace https://bugs.python.org/issue9351 argparse set_defaults on subcommands should override top level set_defaults Due to a change 2012, the subparser gets a new blank `namespace`. When done those values are copied to the main namespace. That gives subparser defaults priority over both the main ones, and the user provided namespace. I don't entirely like that change, but it was made by the original developer. |
|||
| msg376197 - (view) | Author: Lucca Ruhland (lucca.ruhland) | Date: 2020-09-01 16:05 | |
Sorry for the duplication, i haven't noticed the other bug reports. As far as i have seen, i would need to overwrite parts of the _SubParsersAction, _ActionsContainer and ArgumentParser class. Is there maybe an easier way to change the behavior? |
|||
| msg376201 - (view) | Author: paul j3 (paul.j3) * ![]() |
Date: 2020-09-01 18:28 | |
When there are potential conflicts between arguments set by the main parser and those set by a subparser, I often recommend giving the sub ones different "dest" parameters. Then you can reconcile the different values after parsing. There's nothing wrong with a bit of post-parsing value checking and adjustment. The primary purpose of the parser is to identify what the user wants, and secondarily to give useful help and error messages. |
|||
| msg376206 - (view) | Author: Lucca Ruhland (lucca.ruhland) | Date: 2020-09-01 18:56 | |
Thank you very much for your support. I would say we can close this bug report. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:35 | admin | set | github: 85850 |
| 2020-09-21 00:54:43 | paul.j3 | set | status: open -> closed stage: resolved |
| 2020-09-01 18:56:32 | lucca.ruhland | set | resolution: duplicate messages: + msg376206 |
| 2020-09-01 18:28:29 | paul.j3 | set | messages: + msg376201 |
| 2020-09-01 16:05:50 | lucca.ruhland | set | messages: + msg376197 |
| 2020-09-01 15:42:45 | paul.j3 | set | messages: + msg376194 |
| 2020-09-01 09:54:15 | xtreak | set | nosy:
+ rhettinger, paul.j3 |
| 2020-09-01 08:33:31 | lucca.ruhland | create | |


