paul j3 added the comment: A fix that I am exploring would wrap the Action instantiation call in add_argument with a try/except block
That is replace: def add_argument(...) .... action = action_class(**kwargs) ... with .... try: action = action_class(**kwargs) except TypeError: msg = str(_sys.exc_info()[1]) msg = msg.replace('__init__','add_argument') msg = [msg] msg.append('Wrong argument(s) for Action subclass: %s'%action_class.__name__) # action_class is now a class, rather than the input string msg.append('kwargs: %s'%kwargs) sig = getattr(action_class, 'action_sig',None) if sig is not None: msg = sig(msg) raise ValueError(msg) .... This collects information on the error, the action class and arguments, and passes them to a class method of the Action. That customizes the message, and returns it for reraising. In 'class Action' I define: @classmethod def action_sig(cls, msg=[]): # return the signature # subclasses may return custom version import inspect try: name = cls.__name__ sig = inspect.signature(cls.__init__) sig = str(sig) except AttributeError: spec = inspect.getfullargspec(cls.__init__) sig = inspect.formatargspec(*spec) # remove self, option_strings, dest dstr='dest,' ind = sig.find(dstr) if ind>=0: sig = '(...'+sig[(ind+len(dstr)+1):] sig = 'class args: %s'%sig msg.append(sig) return '\n'.join(msg) This adds inspect.signature information from the subclass __init__ method to the message from add_argument. Subclasses (including the user defined ones) could customize this. So a missing 'const' error would display as: ValueError: add_argument() missing 1 required positional argument: 'const' Wrong argument(s) for Action subclass: _StoreConstAction kwargs: {'option_strings': ['--bar'], 'dest': 'bar'} class args: (...const, default=None, required=False, help=None, metavar=None) This may be overkill, but it gives an idea of the information that we can add to the original TypeError. Done right this action_sig() could also serve as a usage function for an Action class. ---------- keywords: +3.5regression _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25299> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com