Skip to content

Commit c40a962

Browse files
committed
Added use of @functools.wraps() in our decorators.
This updates a wrapper function to look like the wrapped function. This partially addresses Issue #271 where using multiple decorators can break help on subcommands, depending on the order in which the decorators are applied. This PR doesn't actually fix anything in and of itself. But it encourages the usage of @functools.wraps(). If other decorators use this, then there shouldn't be any problem. Of course, we can't control how 3rd-party libraries implement decorators. But we can at least be part of the solution instead of part of the problem.
1 parent 656a774 commit c40a962

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

cmd2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import codecs
3131
import collections
3232
import datetime
33+
import functools
3334
import glob
3435
import io
3536
import optparse
@@ -271,6 +272,7 @@ def with_argument_list(func):
271272
method. Default passes a string of whatever the user typed.
272273
With this decorator, the decorated method will receive a list
273274
of arguments parsed from user input using shlex.split()."""
275+
@functools.wraps(func)
274276
def cmd_wrapper(self, cmdline):
275277
lexed_arglist = parse_quoted_string(cmdline)
276278
func(self, lexed_arglist)
@@ -288,6 +290,7 @@ def with_argparser_and_unknown_args(argparser, subcommand_names=None):
288290
:return: function that gets passed parsed args and a list of unknown args
289291
"""
290292
def arg_decorator(func):
293+
@functools.wraps(func)
291294
def cmd_wrapper(instance, cmdline):
292295
lexed_arglist = parse_quoted_string(cmdline)
293296
args, unknown = argparser.parse_known_args(lexed_arglist)
@@ -324,6 +327,7 @@ def with_argparser(argparser, subcommand_names=None):
324327
:return: function that gets passed parsed args
325328
"""
326329
def arg_decorator(func):
330+
@functools.wraps(func)
327331
def cmd_wrapper(instance, cmdline):
328332
lexed_arglist = parse_quoted_string(cmdline)
329333
args = argparser.parse_args(lexed_arglist)
@@ -387,6 +391,7 @@ def option_setup(func):
387391
option_parser.set_usage("%s %s" % (func.__name__[3:], arg_desc))
388392
option_parser._func = func
389393

394+
@functools.wraps(func)
390395
def new_func(instance, arg):
391396
"""For @options commands this replaces the actual do_* methods in the instance __dict__.
392397

0 commit comments

Comments
 (0)