Skip to content

Commit fd8537b

Browse files
authored
Merge pull request gitpython-developers#523 from yarikoptic/enh-wraps
RF: use @functools.wraps within decorators instead of manual __name__ reassignment
2 parents df5c1cb + 51f4a14 commit fd8537b

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

git/config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import abc
1818
import os
1919

20+
from functools import wraps
21+
2022
from git.odict import OrderedDict
2123
from git.util import LockFile
2224
from git.compat import (
@@ -67,11 +69,11 @@ def __new__(metacls, name, bases, clsdict):
6769
def needs_values(func):
6870
"""Returns method assuring we read values (on demand) before we try to access them"""
6971

72+
@wraps(func)
7073
def assure_data_present(self, *args, **kwargs):
7174
self.read()
7275
return func(self, *args, **kwargs)
7376
# END wrapper method
74-
assure_data_present.__name__ = func.__name__
7577
return assure_data_present
7678

7779

git/index/util.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import struct
33
import tempfile
44
import os
5+
6+
from functools import wraps
7+
58
from git.compat import is_win
69

710
__all__ = ('TemporaryFileSwap', 'post_clear_cache', 'default_index', 'git_working_dir')
@@ -48,13 +51,13 @@ def post_clear_cache(func):
4851
natively which in fact is possible, but probably not feasible performance wise.
4952
"""
5053

54+
@wraps(func)
5155
def post_clear_cache_if_not_raised(self, *args, **kwargs):
5256
rval = func(self, *args, **kwargs)
5357
self._delete_entries_cache()
5458
return rval
55-
5659
# END wrapper method
57-
post_clear_cache_if_not_raised.__name__ = func.__name__
60+
5861
return post_clear_cache_if_not_raised
5962

6063

@@ -63,21 +66,22 @@ def default_index(func):
6366
repository index. This is as we rely on git commands that operate
6467
on that index only. """
6568

69+
@wraps(func)
6670
def check_default_index(self, *args, **kwargs):
6771
if self._file_path != self._index_path():
6872
raise AssertionError(
6973
"Cannot call %r on indices that do not represent the default git index" % func.__name__)
7074
return func(self, *args, **kwargs)
7175
# END wrpaper method
7276

73-
check_default_index.__name__ = func.__name__
7477
return check_default_index
7578

7679

7780
def git_working_dir(func):
7881
"""Decorator which changes the current working dir to the one of the git
7982
repository in order to assure relative paths are handled correctly"""
8083

84+
@wraps(func)
8185
def set_git_working_dir(self, *args, **kwargs):
8286
cur_wd = os.getcwd()
8387
os.chdir(self.repo.working_tree_dir)
@@ -88,7 +92,6 @@ def set_git_working_dir(self, *args, **kwargs):
8892
# END handle working dir
8993
# END wrapper
9094

91-
set_git_working_dir.__name__ = func.__name__
9295
return set_git_working_dir
9396

9497
#} END decorators

git/test/lib/helper.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import io
1313
import logging
1414

15+
from functools import wraps
16+
1517
from git import Repo, Remote, GitCommandError, Git
1618
from git.util import rmtree
1719
from git.compat import string_types, is_win
@@ -86,6 +88,7 @@ def with_rw_directory(func):
8688
"""Create a temporary directory which can be written to, remove it if the
8789
test succeeds, but leave it otherwise to aid additional debugging"""
8890

91+
@wraps(func)
8992
def wrapper(self):
9093
path = tempfile.mktemp(prefix=func.__name__)
9194
os.mkdir(path)
@@ -122,6 +125,7 @@ def with_rw_repo(working_tree_ref, bare=False):
122125
assert isinstance(working_tree_ref, string_types), "Decorator requires ref name for working tree checkout"
123126

124127
def argument_passer(func):
128+
@wraps(func)
125129
def repo_creator(self):
126130
prefix = 'non_'
127131
if bare:
@@ -155,7 +159,6 @@ def repo_creator(self):
155159
# END rm test repo if possible
156160
# END cleanup
157161
# END rw repo creator
158-
repo_creator.__name__ = func.__name__
159162
return repo_creator
160163
# END argument passer
161164
return argument_passer
@@ -211,6 +214,7 @@ def case(self, rw_repo, rw_remote_repo)
211214

212215
def argument_passer(func):
213216

217+
@wraps(func)
214218
def remote_repo_creator(self):
215219
remote_repo_dir = _mktemp("remote_repo_%s" % func.__name__)
216220
repo_dir = _mktemp("remote_clone_non_bare_repo")
@@ -319,10 +323,9 @@ def remote_repo_creator(self):
319323
gd.proc.wait()
320324
# END cleanup
321325
# END bare repo creator
322-
remote_repo_creator.__name__ = func.__name__
323326
return remote_repo_creator
324327
# END remote repo creator
325-
# END argument parsser
328+
# END argument parser
326329

327330
return argument_passer
328331

git/util.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import stat
1515
import time
1616

17+
from functools import wraps
18+
1719
from git.compat import is_win
1820
from gitdb.util import ( # NOQA
1921
make_sha,
@@ -50,13 +52,13 @@ def unbare_repo(func):
5052
"""Methods with this decorator raise InvalidGitRepositoryError if they
5153
encounter a bare repository"""
5254

55+
@wraps(func)
5356
def wrapper(self, *args, **kwargs):
5457
if self.repo.bare:
5558
raise InvalidGitRepositoryError("Method '%s' cannot operate on bare repositories" % func.__name__)
5659
# END bare method
5760
return func(self, *args, **kwargs)
5861
# END wrapper
59-
wrapper.__name__ = func.__name__
6062
return wrapper
6163

6264

0 commit comments

Comments
 (0)