9
9
from git .util import to_bin_sha
10
10
11
11
from . import util
12
- from .base import IndexObject
12
+ from .base import IndexObject , IndexObjUnion
13
13
from .blob import Blob
14
14
from .submodule .base import Submodule
15
15
28
28
29
29
if TYPE_CHECKING :
30
30
from git .repo import Repo
31
- from git .objects .util import TraversedTup
32
31
from io import BytesIO
33
32
34
- T_Tree_cache = TypeVar ('T_Tree_cache' , bound = Union [Tuple [bytes , int , str ]])
33
+ T_Tree_cache = TypeVar ('T_Tree_cache' , bound = Tuple [bytes , int , str ])
34
+ TraversedTreeTup = Union [Tuple [Union ['Tree' , None ], IndexObjUnion ,
35
+ Tuple ['Submodule' , 'Submodule' ]]]
35
36
36
37
#--------------------------------------------------------
37
38
@@ -201,7 +202,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
201
202
symlink_id = 0o12
202
203
tree_id = 0o04
203
204
204
- _map_id_to_type : Dict [int , Union [ Type [Submodule ], Type [ Blob ], Type [ 'Tree' ] ]] = {
205
+ _map_id_to_type : Dict [int , Type [IndexObjUnion ]] = {
205
206
commit_id : Submodule ,
206
207
blob_id : Blob ,
207
208
symlink_id : Blob
@@ -229,7 +230,7 @@ def _set_cache_(self, attr: str) -> None:
229
230
# END handle attribute
230
231
231
232
def _iter_convert_to_object (self , iterable : Iterable [Tuple [bytes , int , str ]]
232
- ) -> Iterator [Union [ Blob , 'Tree' , Submodule ] ]:
233
+ ) -> Iterator [IndexObjUnion ]:
233
234
"""Iterable yields tuples of (binsha, mode, name), which will be converted
234
235
to the respective object representation"""
235
236
for binsha , mode , name in iterable :
@@ -240,7 +241,7 @@ def _iter_convert_to_object(self, iterable: Iterable[Tuple[bytes, int, str]]
240
241
raise TypeError ("Unknown mode %o found in tree data for path '%s'" % (mode , path )) from e
241
242
# END for each item
242
243
243
- def join (self , file : str ) -> Union [ Blob , 'Tree' , Submodule ] :
244
+ def join (self , file : str ) -> IndexObjUnion :
244
245
"""Find the named object in this tree's contents
245
246
:return: ``git.Blob`` or ``git.Tree`` or ``git.Submodule``
246
247
@@ -273,7 +274,7 @@ def join(self, file: str) -> Union[Blob, 'Tree', Submodule]:
273
274
raise KeyError (msg % file )
274
275
# END handle long paths
275
276
276
- def __truediv__ (self , file : str ) -> Union [ 'Tree' , Blob , Submodule ] :
277
+ def __truediv__ (self , file : str ) -> IndexObjUnion :
277
278
"""For PY3 only"""
278
279
return self .join (file )
279
280
@@ -296,17 +297,16 @@ def cache(self) -> TreeModifier:
296
297
See the ``TreeModifier`` for more information on how to alter the cache"""
297
298
return TreeModifier (self ._cache )
298
299
299
- def traverse (self ,
300
- predicate : Callable [[Union ['Tree' , 'Submodule' , 'Blob' ,
301
- 'TraversedTup' ], int ], bool ] = lambda i , d : True ,
302
- prune : Callable [[Union ['Tree' , 'Submodule' , 'Blob' , 'TraversedTup' ], int ], bool ] = lambda i , d : False ,
300
+ def traverse (self , # type: ignore # overrides super()
301
+ predicate : Callable [[Union [IndexObjUnion , TraversedTreeTup ], int ], bool ] = lambda i , d : True ,
302
+ prune : Callable [[Union [IndexObjUnion , TraversedTreeTup ], int ], bool ] = lambda i , d : False ,
303
303
depth : int = - 1 ,
304
304
branch_first : bool = True ,
305
305
visit_once : bool = False ,
306
306
ignore_self : int = 1 ,
307
307
as_edge : bool = False
308
- ) -> Union [Iterator [Union [ 'Tree' , 'Blob' , 'Submodule' ] ],
309
- Iterator [Tuple [ Union [ 'Tree' , 'Submodule' , None ], Union [ 'Tree' , 'Blob' , 'Submodule' ]] ]]:
308
+ ) -> Union [Iterator [IndexObjUnion ],
309
+ Iterator [TraversedTreeTup ]]:
310
310
"""For documentation, see util.Traversable._traverse()
311
311
Trees are set to visit_once = False to gain more performance in the traversal"""
312
312
@@ -320,23 +320,22 @@ def traverse(self,
320
320
# ret_tup = itertools.tee(ret, 2)
321
321
# assert is_tree_traversed(ret_tup), f"Type is {[type(x) for x in list(ret_tup[0])]}"
322
322
# return ret_tup[0]"""
323
- return cast (Union [Iterator [Union ['Tree' , 'Blob' , 'Submodule' ]],
324
- Iterator [Tuple [Union ['Tree' , 'Submodule' , None ], Union ['Tree' , 'Blob' , 'Submodule' ]]]],
323
+ return cast (Union [Iterator [IndexObjUnion ], Iterator [TraversedTreeTup ]],
325
324
super (Tree , self ).traverse (predicate , prune , depth , # type: ignore
326
325
branch_first , visit_once , ignore_self ))
327
326
328
327
# List protocol
329
328
330
- def __getslice__ (self , i : int , j : int ) -> List [Union [ Blob , 'Tree' , Submodule ] ]:
329
+ def __getslice__ (self , i : int , j : int ) -> List [IndexObjUnion ]:
331
330
return list (self ._iter_convert_to_object (self ._cache [i :j ]))
332
331
333
- def __iter__ (self ) -> Iterator [Union [ Blob , 'Tree' , Submodule ] ]:
332
+ def __iter__ (self ) -> Iterator [IndexObjUnion ]:
334
333
return self ._iter_convert_to_object (self ._cache )
335
334
336
335
def __len__ (self ) -> int :
337
336
return len (self ._cache )
338
337
339
- def __getitem__ (self , item : Union [str , int , slice ]) -> Union [ Blob , 'Tree' , Submodule ] :
338
+ def __getitem__ (self , item : Union [str , int , slice ]) -> IndexObjUnion :
340
339
if isinstance (item , int ):
341
340
info = self ._cache [item ]
342
341
return self ._map_id_to_type [info [1 ] >> 12 ](self .repo , info [0 ], info [1 ], join_path (self .path , info [2 ]))
@@ -348,7 +347,7 @@ def __getitem__(self, item: Union[str, int, slice]) -> Union[Blob, 'Tree', Submo
348
347
349
348
raise TypeError ("Invalid index type: %r" % item )
350
349
351
- def __contains__ (self , item : Union [IndexObject , PathLike ]) -> bool :
350
+ def __contains__ (self , item : Union [IndexObjUnion , PathLike ]) -> bool :
352
351
if isinstance (item , IndexObject ):
353
352
for info in self ._cache :
354
353
if item .binsha == info [0 ]:
0 commit comments