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: argparse non alphanum characters replacement
Type: enhancement Stage:
Components: None Versions: Python 3.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: bethard, brmzkw, r.david.murray
Priority: normal Keywords: patch

Created on 2012-09-12 09:00 by brmzkw, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
argparse.patch brmzkw, 2012-09-12 09:00
Messages (5)
msg170361 - (view) Author: Julien Castets (brmzkw) Date: 2012-09-12 09:00
argparse.add_argument replaces dashes with underscores. If an argument contains another non alphanum character, accessing to it will result to a syntax error.

#! /usr/bin/env python

import argparse

if __name__ == '__main__':
    argParser = argparse.ArgumentParser()
    argParser.add_argument('--foo+', action='store_true')
    arguments = argParser.parse_args()

    print 'getattr foo+: %s' % getattr(arguments, 'foo+') # ok
    print arguments.foo+ # syntax error

The patch replaces every non alnum character with an underscore.
msg170363 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-12 10:24
It would probably be better to have the namespace object support subscripting in order to satisfy this use case.  You can use getattr to do it now.  (Note that the namespace object should probably use a real dict and a __getattr__ method, since I don't think the language guarantees that you can put non-identifiers in an attribute dict).
msg170364 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-09-12 10:38
If you need to get things that aren't valid Python identifiers, use vars() to get the dictionary:

http://docs.python.org/dev/library/argparse.html#the-namespace-object

Changing all non-alphanumeric characters to underscores would be a backwards incompatible change for argparse (it would break existing code). That means we'd need to have a long deprecation period before the change. I'm not sure it's really worth it for this feature when vars() already gives you what you need easily.

I'm therefore closing this a "won't fix", but if you feel really strongly that vars() doesn't solve your problem and you want to push through the long deprecation process, feel free to re-open.

@R. David Murray: I wouldn't worry about non-identifiers in an attribute dict. This has worked for ages, and changing this would be a seriously backwards incompatible change that couldn't be possible before Python 4.
msg170367 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-12 11:49
Oh, it wasn't CPython that that comment was directed at.  But I think you are right: because CPython supports it, I think other implementations will as well, whatever the language spec says or doesn't say (I didn't double check, I'm going on a fuzzy memory of a conversation on python-dev from the pypy folks).
msg170371 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-09-12 12:05
I haven't been following python-dev recently, but the only discussion I remember was for non-strings in __dict__, not non-identifiers.
History
Date User Action Args
2022-04-11 14:57:35adminsetgithub: 60133
2012-09-12 12:05:55bethardsetmessages: + msg170371
2012-09-12 11:49:52r.david.murraysetmessages: + msg170367
2012-09-12 10:38:42bethardsetstatus: open -> closed
resolution: wont fix
messages: + msg170364
2012-09-12 10:24:35r.david.murraysetversions: - Python 3.1, Python 2.7, Python 3.2, Python 3.3
nosy: + r.david.murray, bethard

messages: + msg170363

type: behavior -> enhancement
2012-09-12 09:00:36brmzkwcreate