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.

classification
Title: Multiple names can target the same namespace
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Sworddragon, paul.j3, r.david.murray
Priority: normal Keywords:

Created on 2015-10-03 21:55 by Sworddragon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg252245 - (view) Author: (Sworddragon) Date: 2015-10-03 21:55
In the argparse module I'm noticing that for example an optional argument and a positional argument can target the same namespace. Here is a testcase:

#!/usr/bin/python3 -BEOObbs
# coding=utf-8
import argparse
arguments = argparse.ArgumentParser()
arguments.add_argument('test', action = 'store_true', help = 'Description')
arguments.add_argument('--test', action = 'store_true', help = 'Description')
print(arguments.parse_args())


Maybe if argparse finds out that adding an argument would use the same namespace of an already added argument an exception should be thrown?
msg252268 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-10-04 15:21
I'd be willing to bet there are people using this as a feature.  I don't think we should make any change here.
msg252434 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-10-06 21:03
What you are asking about is the fact that different arguments can share a 'dest'.

https://docs.python.org/3/library/argparse.html#dest

The 'append_const' action has an example of shared 'dest'.  It says:

    The 'append_const' action is typically useful when multiple    
    arguments need to store constants to the same list. 

I've suggested 'dest' sharing in some SO questions.

Shared 'dest' may be a nuisance in the case of regular 'store' actions, since only the last value is retained.  But that also happens if a user repeats an argument.  Usually errors like this become apparent during development testing, and can be easily fixed with changes to the 'dest' parameter.

The 'conflict handler' mechanism does prevent conflicts in the option strings.  But it does not check the 'dest'.  In fact the parser does not maintain a list or dictionary of 'dest'.  It looks that up on the Actions as needed.

A simple function that would collect actions by 'dest' could use:

     dd = collections.defaultdict(list)
     for a in parser._actions:
         dd[a.dest].append(a)

Or the developer could collect their own list of Actions.  So it is easy to test for repeated 'dest', but I don't think it is generally needed or useful.

Custom Action classes can also be used to test for repeat assignments to a given 'dest' (objecting, for example, if the existing value in the Namespace is not the default).
History
Date User Action Args
2022-04-11 14:58:22adminsetgithub: 69495
2015-10-07 02:50:01benjamin.petersonsetstatus: open -> closed
resolution: not a bug
2015-10-06 21:03:02paul.j3setnosy: + paul.j3
messages: + msg252434
2015-10-04 15:21:44r.david.murraysetnosy: + r.david.murray
messages: + msg252268
2015-10-03 21:55:16Sworddragoncreate