1515
1616# typing ------------------------------------------------------------------
1717
18- from typing import Any , Iterator , List , Match , Optional , Tuple , Type , Union , TYPE_CHECKING , cast
18+ from typing import Any , Iterator , List , Match , Optional , Tuple , Type , TypeVar , Union , TYPE_CHECKING
1919from git .types import PathLike , TBD , Literal , TypeGuard
2020
2121if TYPE_CHECKING :
2222 from .objects .tree import Tree
2323 from git .repo .base import Repo
24+ from git .objects .base import IndexObject
2425
2526 from subprocess import Popen
2627
@@ -175,7 +176,10 @@ def diff(self, other: Union[Type[Index], Type['Tree'], object, None, str] = Inde
175176 return index
176177
177178
178- class DiffIndex (list ):
179+ T_Diff = TypeVar ('T_Diff' , bound = 'Diff' )
180+
181+
182+ class DiffIndex (List [T_Diff ]):
179183
180184 """Implements an Index for diffs, allowing a list of Diffs to be queried by
181185 the diff properties.
@@ -189,7 +193,7 @@ class DiffIndex(list):
189193 # T = Changed in the type
190194 change_type = ("A" , "C" , "D" , "R" , "M" , "T" )
191195
192- def iter_change_type (self , change_type : Lit_change_type ) -> Iterator ['Diff' ]:
196+ def iter_change_type (self , change_type : Lit_change_type ) -> Iterator [T_Diff ]:
193197 """
194198 :return:
195199 iterator yielding Diff instances that match the given change_type
@@ -207,8 +211,6 @@ def iter_change_type(self, change_type: Lit_change_type) -> Iterator['Diff']:
207211 raise ValueError ("Invalid change type: %s" % change_type )
208212
209213 for diff in self :
210- diff = cast ('Diff' , diff )
211-
212214 if diff .change_type == change_type :
213215 yield diff
214216 elif change_type == "A" and diff .new_file :
@@ -289,7 +291,7 @@ def __init__(self, repo: 'Repo',
289291 a_mode : Union [bytes , str , None ], b_mode : Union [bytes , str , None ],
290292 new_file : bool , deleted_file : bool , copied_file : bool ,
291293 raw_rename_from : Optional [bytes ], raw_rename_to : Optional [bytes ],
292- diff : Union [str , bytes , None ], change_type : Optional [str ], score : Optional [int ]) -> None :
294+ diff : Union [str , bytes , None ], change_type : Optional [Lit_change_type ], score : Optional [int ]) -> None :
293295
294296 assert a_rawpath is None or isinstance (a_rawpath , bytes )
295297 assert b_rawpath is None or isinstance (b_rawpath , bytes )
@@ -308,19 +310,21 @@ def __init__(self, repo: 'Repo',
308310 repo = submodule .module ()
309311 break
310312
313+ self .a_blob : Union ['IndexObject' , None ]
311314 if a_blob_id is None or a_blob_id == self .NULL_HEX_SHA :
312315 self .a_blob = None
313316 else :
314317 self .a_blob = Blob (repo , hex_to_bin (a_blob_id ), mode = self .a_mode , path = self .a_path )
315318
319+ self .b_blob : Union ['IndexObject' , None ]
316320 if b_blob_id is None or b_blob_id == self .NULL_HEX_SHA :
317321 self .b_blob = None
318322 else :
319323 self .b_blob = Blob (repo , hex_to_bin (b_blob_id ), mode = self .b_mode , path = self .b_path )
320324
321- self .new_file = new_file
322- self .deleted_file = deleted_file
323- self .copied_file = copied_file
325+ self .new_file : bool = new_file
326+ self .deleted_file : bool = deleted_file
327+ self .copied_file : bool = copied_file
324328
325329 # be clear and use None instead of empty strings
326330 assert raw_rename_from is None or isinstance (raw_rename_from , bytes )
@@ -329,7 +333,7 @@ def __init__(self, repo: 'Repo',
329333 self .raw_rename_to = raw_rename_to or None
330334
331335 self .diff = diff
332- self .change_type = change_type
336+ self .change_type : Union [ Lit_change_type , None ] = change_type
333337 self .score = score
334338
335339 def __eq__ (self , other : object ) -> bool :
@@ -449,7 +453,7 @@ def _index_from_patch_format(cls, repo: 'Repo', proc: TBD) -> DiffIndex:
449453
450454 # for now, we have to bake the stream
451455 text = b'' .join (text_list )
452- index = DiffIndex ()
456+ index : 'DiffIndex' = DiffIndex ()
453457 previous_header = None
454458 header = None
455459 a_path , b_path = None , None # for mypy
@@ -560,7 +564,7 @@ def _index_from_raw_format(cls, repo: 'Repo', proc: 'Popen') -> 'DiffIndex':
560564 # handles
561565 # :100644 100644 687099101... 37c5e30c8... M .gitignore
562566
563- index = DiffIndex ()
567+ index : 'DiffIndex' = DiffIndex ()
564568 handle_process_output (proc , lambda byt : cls ._handle_diff_line (byt , repo , index ),
565569 None , finalize_process , decode_streams = False )
566570
0 commit comments