[issue46101] argparse: using parents & subcommands, options can be ignored

2022-02-02 Thread paul j3


Change by paul j3 :


--
resolution:  -> rejected
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46101] argparse: using parents & subcommands, options can be ignored

2022-01-10 Thread paul j3


paul j3  added the comment:

This patch should be rejected.

By using `common_opts_parser` as parent to both the main and subparsers, you 
have defined the same argument in both.

By a long standing patch, the value assigned in the subparser has precedence 
(whether it's the default or user supplied).

https://bugs.python.org/issue9351
argparse set_defaults on subcommands should override top level set_defaults

Partial retraction of this override has been explored, but any changes are 
still up for debate

https://bugs.python.org/issue45235
argparse does not preserve namespace with subparser defaults

The bottom line is that we should not try to define the same argument (or more 
specifically the same `dest`) in both main and subparsers.  Duplicate flags are 
ok.  

But resolving the conflicting values should be done after parsing.  Some users 
will want to give priority to the main parser's values, others to the 
subparser's.  Or their own defaults.  argparse cannot handle all cases cleanly.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46101] argparse: using parents & subcommands, options can be ignored

2022-01-01 Thread paul j3


Change by paul j3 :


--
nosy: +paul.j3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46101] argparse: using parents & subcommands, options can be ignored

2021-12-16 Thread Lucas Cimon


Lucas Cimon  added the comment:

The GitHub PR is ready for reviewing.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46101] argparse: using parents & subcommands, options can be ignored

2021-12-16 Thread Lucas Cimon


Change by Lucas Cimon :


--
keywords: +patch
pull_requests: +28364
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/30146

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46101] argparse: using parents & subcommands, options can be ignored

2021-12-16 Thread Lucas Cimon


New submission from Lucas Cimon :

Hi!

Here is some minimal code reproducing the issue:

import argparse

common_opts_parser = argparse.ArgumentParser(add_help=False)
common_opts_parser.add_argument("--endpoint", choices=("prod", "dev"), 
default="dev")

parser = argparse.ArgumentParser(parents=[common_opts_parser])
subparsers = parser.add_subparsers(required=True)

subcmd_cmd = subparsers.add_parser("subcmd", parents=[common_opts_parser])
subcmd_cmd.add_argument("--debug", action="store_true")

print(parser.parse_args())

Everything works fine / as expected when specifying the common optional arg 
last:

$ ./bug_repro.py subcmd --endpoint=dev
Namespace(endpoint='dev', debug=False)

However when specifying the --endpoint between the program and the subcommand, 
the value provided is ignored:

$ ./bug_repro.py --endpoint=dev subcmd
Namespace(endpoint=None, debug=False)

I have a PR ready to fix that.

--
components: Library (Lib)
messages: 408711
nosy: Lucas Cimon
priority: normal
severity: normal
status: open
title: argparse: using parents & subcommands, options can be ignored
type: behavior
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com