[docs] [issue22401] argparse: 'resolve' conflict handler damages the actions of the parent parser

paul j3 report at bugs.python.org
Thu Oct 2 00:56:07 CEST 2014

paul j3 added the comment:

A simpler solution is to make a copy of each Action when importing a parent parser.  The current practice is to just copy references.  With a copy, an Action will belong to only one group and parser, and the 'resolve' handler will operate without problems.

In the attached patch, I added a `.copy` method to Action.  I believe '.option_strings' is the only attribute that needs special handling, since it is a list, and 'resolve' operates on it.  The other attributes are strings, or objects that the user defines (e.g. 'default', 'choices').

The other change is in '_add_container_actions', the method which imports parents.  Here I make the copy action contingent on a 'action_copy' attribute of the conflict handler object (a function).  I also define this attribute for the 'resolve' handler.  I've made this copy contingent just to be safe w/r to backward compatibility, even though, I can't think of a reason for preferring the existing copy by reference.

In another Stackoverflow question, a poster wanted to use the same parent for 2 subparsers, but give the 2 actions different defaults.  This copy approach solves that issue.

This patch needs testing and documentation changes.

keywords: +patch
Added file: http://bugs.python.org/file36773/patch_1.diff

Python tracker <report at bugs.python.org>

More information about the docs mailing list