99import os .path as osp
1010
1111
12+ # typing ----------------------------------------------------------------------
13+
14+ from typing import (Any , Callable , List , Sequence , TYPE_CHECKING , Tuple , cast )
15+
16+ from git .types import PathLike
17+
18+ if TYPE_CHECKING :
19+ from git .repo import Repo
20+
21+ # ---------------------------------------------------------------------------------
22+
23+
1224__all__ = ('TemporaryFileSwap' , 'post_clear_cache' , 'default_index' , 'git_working_dir' )
1325
1426#{ Aliases
@@ -24,16 +36,16 @@ class TemporaryFileSwap(object):
2436 and moving it back on to where on object deletion."""
2537 __slots__ = ("file_path" , "tmp_file_path" )
2638
27- def __init__ (self , file_path ) :
39+ def __init__ (self , file_path : PathLike ) -> None :
2840 self .file_path = file_path
29- self .tmp_file_path = self .file_path + tempfile .mktemp ('' , '' , '' )
41+ self .tmp_file_path = str ( self .file_path ) + tempfile .mktemp ('' , '' , '' )
3042 # it may be that the source does not exist
3143 try :
3244 os .rename (self .file_path , self .tmp_file_path )
3345 except OSError :
3446 pass
3547
36- def __del__ (self ):
48+ def __del__ (self ) -> None :
3749 if osp .isfile (self .tmp_file_path ):
3850 if is_win and osp .exists (self .file_path ):
3951 os .remove (self .file_path )
@@ -43,7 +55,7 @@ def __del__(self):
4355
4456#{ Decorators
4557
46- def post_clear_cache (func ) :
58+ def post_clear_cache (func : Callable [..., Any ]) -> Callable [..., Any ] :
4759 """Decorator for functions that alter the index using the git command. This would
4860 invalidate our possibly existing entries dictionary which is why it must be
4961 deleted to allow it to be lazily reread later.
@@ -54,7 +66,7 @@ def post_clear_cache(func):
5466 """
5567
5668 @wraps (func )
57- def post_clear_cache_if_not_raised (self , * args , ** kwargs ) :
69+ def post_clear_cache_if_not_raised (self , * args : Any , ** kwargs : Any ) -> Any :
5870 rval = func (self , * args , ** kwargs )
5971 self ._delete_entries_cache ()
6072 return rval
@@ -63,13 +75,13 @@ def post_clear_cache_if_not_raised(self, *args, **kwargs):
6375 return post_clear_cache_if_not_raised
6476
6577
66- def default_index (func ) :
78+ def default_index (func : Callable [..., Any ]) -> Callable [..., Any ] :
6779 """Decorator assuring the wrapped method may only run if we are the default
6880 repository index. This is as we rely on git commands that operate
6981 on that index only. """
7082
7183 @wraps (func )
72- def check_default_index (self , * args , ** kwargs ) :
84+ def check_default_index (self , * args : Any , ** kwargs : Any ) -> Any :
7385 if self ._file_path != self ._index_path ():
7486 raise AssertionError (
7587 "Cannot call %r on indices that do not represent the default git index" % func .__name__ )
@@ -79,12 +91,12 @@ def check_default_index(self, *args, **kwargs):
7991 return check_default_index
8092
8193
82- def git_working_dir (func ) :
94+ def git_working_dir (func : Callable [..., Any ]) -> Callable [..., None ] :
8395 """Decorator which changes the current working dir to the one of the git
8496 repository in order to assure relative paths are handled correctly"""
8597
8698 @wraps (func )
87- def set_git_working_dir (self , * args , ** kwargs ) :
99+ def set_git_working_dir (self , * args : Any , ** kwargs : Any ) -> None :
88100 cur_wd = os .getcwd ()
89101 os .chdir (self .repo .working_tree_dir )
90102 try :
0 commit comments