homepage

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: "append" action fails to override default values
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bethard, eric.smith, guilherme-pg, r.david.murray
Priority: normal Keywords:

Created on 2012年03月09日 00:48 by guilherme-pg, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
t1.py guilherme-pg, 2012年03月09日 00:48 A test case that triggers the bug
Messages (3)
msg155195 - (view) Author: Guilherme Gonçalves (guilherme-pg) Date: 2012年03月09日 00:48
Trying to set a default value to arguments whose action is "append" causes argparse to raise AttributeError when such arguments are provided in the command line (see attached test case t1.py).
This happens because _AppendAction doesn't expect the presence of a default value for the argument: when the command line is parsed and the argument is found, _AppendAction attempts to append the new value to the list of old values, if any, or the old list. In case there is already a non-list default value, it attempts to append the new value to it, which raises the exception.
Is this intended behavior? If so, shouldn't ArgumentError be raised instead? It should also be easy to fix this issue otherwise, making _StoreAction ensure the old values are stored in a list before appending.
msg155203 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2012年03月09日 02:20
I don't think it's worth checking that you've passed in the correct type for the default parameter. That's not something that Python code typically does.
The error you get:
AttributeError: 'tuple' object has no attribute 'append'
seem pretty clear. Pass in a list instead of a tuple, and it works as expected.
msg155211 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012年03月09日 08:12
I agree with Eric. I've run in to this error, and immediatly figured out what I'd done wrong based on the existing error message.
History
Date User Action Args
2022年04月11日 14:57:27adminsetgithub: 58441
2012年03月09日 08:12:44r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg155211

resolution: not a bug
stage: resolved
2012年03月09日 02:20:15eric.smithsetnosy: + eric.smith
messages: + msg155203
2012年03月09日 01:52:56guilherme-pgsettype: crash -> behavior
2012年03月09日 00:48:33guilherme-pgcreate

AltStyle によって変換されたページ (->オリジナル) /