20
20
21
21
# typing -------------------------------------------------
22
22
23
- from typing import Callable , Dict , Iterable , Iterator , List , Tuple , Type , Union , cast , TYPE_CHECKING
23
+ from typing import Callable , Dict , Generic , Iterable , Iterator , List , Tuple , Type , TypeVar , Union , cast , TYPE_CHECKING
24
24
25
25
from git .types import PathLike
26
26
31
31
#--------------------------------------------------------
32
32
33
33
34
- cmp : Callable [[int , int ], int ] = lambda a , b : (a > b ) - (a < b )
34
+ cmp : Callable [[str , str ], int ] = lambda a , b : (a > b ) - (a < b )
35
35
36
36
__all__ = ("TreeModifier" , "Tree" )
37
37
38
+ T_Tree_cache = TypeVar ('T_Tree_cache' , bound = Union [Tuple [bytes , int , str ]])
38
39
39
- def git_cmp (t1 : 'Tree' , t2 : 'Tree' ) -> int :
40
+
41
+ def git_cmp (t1 : T_Tree_cache , t2 : T_Tree_cache ) -> int :
40
42
a , b = t1 [2 ], t2 [2 ]
43
+ assert isinstance (a , str ) and isinstance (b , str ) # Need as mypy 9.0 cannot unpack TypeVar properly
41
44
len_a , len_b = len (a ), len (b )
42
45
min_len = min (len_a , len_b )
43
46
min_cmp = cmp (a [:min_len ], b [:min_len ])
@@ -48,7 +51,8 @@ def git_cmp(t1: 'Tree', t2: 'Tree') -> int:
48
51
return len_a - len_b
49
52
50
53
51
- def merge_sort (a : List [int ], cmp : Callable [[int , int ], int ]) -> None :
54
+ def merge_sort (a : List [T_Tree_cache ],
55
+ cmp : Callable [[T_Tree_cache , T_Tree_cache ], int ]) -> None :
52
56
if len (a ) < 2 :
53
57
return None
54
58
@@ -83,18 +87,18 @@ def merge_sort(a: List[int], cmp: Callable[[int, int], int]) -> None:
83
87
k = k + 1
84
88
85
89
86
- class TreeModifier (object ):
90
+ class TreeModifier (Generic [ T_Tree_cache ], object ):
87
91
88
92
"""A utility class providing methods to alter the underlying cache in a list-like fashion.
89
93
90
94
Once all adjustments are complete, the _cache, which really is a reference to
91
95
the cache of a tree, will be sorted. Assuring it will be in a serializable state"""
92
96
__slots__ = '_cache'
93
97
94
- def __init__ (self , cache ) :
98
+ def __init__ (self , cache : List [ T_Tree_cache ]) -> None :
95
99
self ._cache = cache
96
100
97
- def _index_by_name (self , name ) :
101
+ def _index_by_name (self , name : str ) -> int :
98
102
""":return: index of an item with name, or -1 if not found"""
99
103
for i , t in enumerate (self ._cache ):
100
104
if t [2 ] == name :
@@ -104,7 +108,7 @@ def _index_by_name(self, name):
104
108
return - 1
105
109
106
110
#{ Interface
107
- def set_done (self ):
111
+ def set_done (self ) -> 'TreeModifier' :
108
112
"""Call this method once you are done modifying the tree information.
109
113
It may be called several times, but be aware that each call will cause
110
114
a sort operation
@@ -114,7 +118,7 @@ def set_done(self):
114
118
#} END interface
115
119
116
120
#{ Mutators
117
- def add (self , sha , mode , name , force = False ):
121
+ def add (self , sha : bytes , mode : int , name : str , force : bool = False ) -> 'TreeModifier' :
118
122
"""Add the given item to the tree. If an item with the given name already
119
123
exists, nothing will be done, but a ValueError will be raised if the
120
124
sha and mode of the existing item do not match the one you add, unless
@@ -132,7 +136,7 @@ def add(self, sha, mode, name, force=False):
132
136
133
137
sha = to_bin_sha (sha )
134
138
index = self ._index_by_name (name )
135
- item = (sha , mode , name )
139
+ item : T_Tree_cache = (sha , mode , name ) # type: ignore ## use Typeguard from typing-extensions 3.10.0
136
140
if index == - 1 :
137
141
self ._cache .append (item )
138
142
else :
@@ -195,7 +199,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
195
199
def __init__ (self , repo : 'Repo' , binsha : bytes , mode : int = tree_id << 12 , path : Union [PathLike , None ] = None ):
196
200
super (Tree , self ).__init__ (repo , binsha , mode , path )
197
201
198
- @classmethod
202
+ @ classmethod
199
203
def _get_intermediate_items (cls , index_object : 'Tree' , # type: ignore
200
204
) -> Union [Tuple ['Tree' , ...], Tuple [()]]:
201
205
if index_object .type == "tree" :
@@ -261,17 +265,17 @@ def __truediv__(self, file: str) -> Union['Tree', Blob, Submodule]:
261
265
"""For PY3 only"""
262
266
return self .join (file )
263
267
264
- @property
268
+ @ property
265
269
def trees (self ) -> List ['Tree' ]:
266
270
""":return: list(Tree, ...) list of trees directly below this tree"""
267
271
return [i for i in self if i .type == "tree" ]
268
272
269
- @property
273
+ @ property
270
274
def blobs (self ) -> List ['Blob' ]:
271
275
""":return: list(Blob, ...) list of blobs directly below this tree"""
272
276
return [i for i in self if i .type == "blob" ]
273
277
274
- @property
278
+ @ property
275
279
def cache (self ) -> TreeModifier :
276
280
"""
277
281
:return: An object allowing to modify the internal cache. This can be used
0 commit comments