Skip to content

RF: use @functools.wraps within decorators instead of manual __name__ reassignment #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
11 changes: 7 additions & 4 deletions git/index/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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


Expand All @@ -63,21 +66,22 @@ 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(
"Cannot call %r on indices that do not represent the default git index" % func.__name__)
return func(self, *args, **kwargs)
# END wrpaper method

check_default_index.__name__ = func.__name__
return check_default_index


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)
Expand All @@ -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
9 changes: 6 additions & 3 deletions git/test/lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down