99from git .util import to_bin_sha
1010
1111from . import util
12- from .base import IndexObject
12+ from .base import IndexObject , IndexObjUnion
1313from .blob import Blob
1414from .submodule .base import Submodule
1515
2828
2929if TYPE_CHECKING :
3030 from git .repo import Repo
31- from git .objects .util import TraversedTup
3231 from io import BytesIO
3332
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' ]]]
3536
3637#--------------------------------------------------------
3738
@@ -201,7 +202,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
201202 symlink_id = 0o12
202203 tree_id = 0o04
203204
204- _map_id_to_type : Dict [int , Union [ Type [Submodule ], Type [ Blob ], Type [ 'Tree' ] ]] = {
205+ _map_id_to_type : Dict [int , Type [IndexObjUnion ]] = {
205206 commit_id : Submodule ,
206207 blob_id : Blob ,
207208 symlink_id : Blob
@@ -229,7 +230,7 @@ def _set_cache_(self, attr: str) -> None:
229230 # END handle attribute
230231
231232 def _iter_convert_to_object (self , iterable : Iterable [Tuple [bytes , int , str ]]
232- ) -> Iterator [Union [ Blob , 'Tree' , Submodule ] ]:
233+ ) -> Iterator [IndexObjUnion ]:
233234 """Iterable yields tuples of (binsha, mode, name), which will be converted
234235 to the respective object representation"""
235236 for binsha , mode , name in iterable :
@@ -240,7 +241,7 @@ def _iter_convert_to_object(self, iterable: Iterable[Tuple[bytes, int, str]]
240241 raise TypeError ("Unknown mode %o found in tree data for path '%s'" % (mode , path )) from e
241242 # END for each item
242243
243- def join (self , file : str ) -> Union [ Blob , 'Tree' , Submodule ] :
244+ def join (self , file : str ) -> IndexObjUnion :
244245 """Find the named object in this tree's contents
245246 :return: ``git.Blob`` or ``git.Tree`` or ``git.Submodule``
246247
@@ -273,7 +274,7 @@ def join(self, file: str) -> Union[Blob, 'Tree', Submodule]:
273274 raise KeyError (msg % file )
274275 # END handle long paths
275276
276- def __truediv__ (self , file : str ) -> Union [ 'Tree' , Blob , Submodule ] :
277+ def __truediv__ (self , file : str ) -> IndexObjUnion :
277278 """For PY3 only"""
278279 return self .join (file )
279280
@@ -296,17 +297,16 @@ def cache(self) -> TreeModifier:
296297 See the ``TreeModifier`` for more information on how to alter the cache"""
297298 return TreeModifier (self ._cache )
298299
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 ,
303303 depth : int = - 1 ,
304304 branch_first : bool = True ,
305305 visit_once : bool = False ,
306306 ignore_self : int = 1 ,
307307 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 ]]:
310310 """For documentation, see util.Traversable._traverse()
311311 Trees are set to visit_once = False to gain more performance in the traversal"""
312312
@@ -320,23 +320,22 @@ def traverse(self,
320320 # ret_tup = itertools.tee(ret, 2)
321321 # assert is_tree_traversed(ret_tup), f"Type is {[type(x) for x in list(ret_tup[0])]}"
322322 # 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 ]],
325324 super (Tree , self ).traverse (predicate , prune , depth , # type: ignore
326325 branch_first , visit_once , ignore_self ))
327326
328327 # List protocol
329328
330- def __getslice__ (self , i : int , j : int ) -> List [Union [ Blob , 'Tree' , Submodule ] ]:
329+ def __getslice__ (self , i : int , j : int ) -> List [IndexObjUnion ]:
331330 return list (self ._iter_convert_to_object (self ._cache [i :j ]))
332331
333- def __iter__ (self ) -> Iterator [Union [ Blob , 'Tree' , Submodule ] ]:
332+ def __iter__ (self ) -> Iterator [IndexObjUnion ]:
334333 return self ._iter_convert_to_object (self ._cache )
335334
336335 def __len__ (self ) -> int :
337336 return len (self ._cache )
338337
339- def __getitem__ (self , item : Union [str , int , slice ]) -> Union [ Blob , 'Tree' , Submodule ] :
338+ def __getitem__ (self , item : Union [str , int , slice ]) -> IndexObjUnion :
340339 if isinstance (item , int ):
341340 info = self ._cache [item ]
342341 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
348347
349348 raise TypeError ("Invalid index type: %r" % item )
350349
351- def __contains__ (self , item : Union [IndexObject , PathLike ]) -> bool :
350+ def __contains__ (self , item : Union [IndexObjUnion , PathLike ]) -> bool :
352351 if isinstance (item , IndexObject ):
353352 for info in self ._cache :
354353 if item .binsha == info [0 ]:
0 commit comments