From 51f4a1407ef12405e16f643f5f9d2002b4b52ab9 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Sun, 2 Oct 2016 11:17:36 -0400 Subject: [PATCH] RF: use @functools.wraps within decorators instead of manual __name__ reassignment @wraps does more and does it right ;) --- git/config.py | 4 +++- git/index/util.py | 11 +++++++---- git/test/lib/helper.py | 9 ++++++--- git/util.py | 4 +++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/git/config.py b/git/config.py index ad6192ff2..b342410c8 100644 --- a/git/config.py +++ b/git/config.py @@ -17,6 +17,8 @@ import abc import os +from functools import wraps + from git.odict import OrderedDict from git.util import LockFile from git.compat import ( @@ -67,11 +69,11 @@ def __new__(metacls, name, bases, clsdict): def needs_values(func): """Returns method assuring we read values (on demand) before we try to access them""" + @wraps(func) def assure_data_present(self, *args, **kwargs): self.read() return func(self, *args, **kwargs) # END wrapper method - assure_data_present.__name__ = func.__name__ return assure_data_present diff --git a/git/index/util.py b/git/index/util.py index 0340500cc..ce798851d 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -2,6 +2,9 @@ import struct import tempfile import os + +from functools import wraps + from git.compat import is_win __all__ = ('TemporaryFileSwap', 'post_clear_cache', 'default_index', 'git_working_dir') @@ -48,13 +51,13 @@ def post_clear_cache(func): natively which in fact is possible, but probably not feasible performance wise. """ + @wraps(func) def post_clear_cache_if_not_raised(self, *args, **kwargs): rval = func(self, *args, **kwargs) self._delete_entries_cache() return rval - # END wrapper method - post_clear_cache_if_not_raised.__name__ = func.__name__ + return post_clear_cache_if_not_raised @@ -63,6 +66,7 @@ def default_index(func): repository index. This is as we rely on git commands that operate on that index only. """ + @wraps(func) def check_default_index(self, *args, **kwargs): if self._file_path != self._index_path(): raise AssertionError( @@ -70,7 +74,6 @@ def check_default_index(self, *args, **kwargs): return func(self, *args, **kwargs) # END wrpaper method - check_default_index.__name__ = func.__name__ return check_default_index @@ -78,6 +81,7 @@ def git_working_dir(func): """Decorator which changes the current working dir to the one of the git repository in order to assure relative paths are handled correctly""" + @wraps(func) def set_git_working_dir(self, *args, **kwargs): cur_wd = os.getcwd() os.chdir(self.repo.working_tree_dir) @@ -88,7 +92,6 @@ def set_git_working_dir(self, *args, **kwargs): # END handle working dir # END wrapper - set_git_working_dir.__name__ = func.__name__ return set_git_working_dir #} END decorators diff --git a/git/test/lib/helper.py b/git/test/lib/helper.py index a85ac2fd6..e55a23df4 100644 --- a/git/test/lib/helper.py +++ b/git/test/lib/helper.py @@ -12,6 +12,8 @@ import io import logging +from functools import wraps + from git import Repo, Remote, GitCommandError, Git from git.util import rmtree from git.compat import string_types, is_win @@ -86,6 +88,7 @@ def with_rw_directory(func): """Create a temporary directory which can be written to, remove it if the test succeeds, but leave it otherwise to aid additional debugging""" + @wraps(func) def wrapper(self): path = tempfile.mktemp(prefix=func.__name__) os.mkdir(path) @@ -122,6 +125,7 @@ def with_rw_repo(working_tree_ref, bare=False): assert isinstance(working_tree_ref, string_types), "Decorator requires ref name for working tree checkout" def argument_passer(func): + @wraps(func) def repo_creator(self): prefix = 'non_' if bare: @@ -155,7 +159,6 @@ def repo_creator(self): # END rm test repo if possible # END cleanup # END rw repo creator - repo_creator.__name__ = func.__name__ return repo_creator # END argument passer return argument_passer @@ -211,6 +214,7 @@ def case(self, rw_repo, rw_remote_repo) def argument_passer(func): + @wraps(func) def remote_repo_creator(self): remote_repo_dir = _mktemp("remote_repo_%s" % func.__name__) repo_dir = _mktemp("remote_clone_non_bare_repo") @@ -319,10 +323,9 @@ def remote_repo_creator(self): gd.proc.wait() # END cleanup # END bare repo creator - remote_repo_creator.__name__ = func.__name__ return remote_repo_creator # END remote repo creator - # END argument parsser + # END argument parser return argument_passer diff --git a/git/util.py b/git/util.py index 814cd7f46..9640a74f4 100644 --- a/git/util.py +++ b/git/util.py @@ -14,6 +14,8 @@ import stat import time +from functools import wraps + from git.compat import is_win from gitdb.util import ( # NOQA make_sha, @@ -50,13 +52,13 @@ def unbare_repo(func): """Methods with this decorator raise InvalidGitRepositoryError if they encounter a bare repository""" + @wraps(func) def wrapper(self, *args, **kwargs): if self.repo.bare: raise InvalidGitRepositoryError("Method '%s' cannot operate on bare repositories" % func.__name__) # END bare method return func(self, *args, **kwargs) # END wrapper - wrapper.__name__ = func.__name__ return wrapper