1616# typing ------------------------------------------------------------------
1717
1818from typing import Any , Iterator , List , Match , Optional , Tuple , Type , Union , TYPE_CHECKING
19- from git .types import PathLike , TBD , Literal
19+ from git .types import PathLike , TBD , Literal , TypeGuard
2020
2121if TYPE_CHECKING :
2222from .objects .tree import Tree
@@ -200,7 +200,8 @@ def iter_change_type(self, change_type: Lit_change_type) -> Iterator['Diff']:
200200if change_type not in self .change_type :
201201raise ValueError ("Invalid change type: %s" % change_type )
202202
203- for diff in self : # type: 'Diff'
203+ # diff: 'Diff'
204+ for diff in self :
204205if diff .change_type == change_type :
205206yield diff
206207elif change_type == "A" and diff .new_file :
@@ -281,7 +282,8 @@ def __init__(self, repo: 'Repo',
281282a_mode : Union [bytes , str , None ], b_mode : Union [bytes , str , None ],
282283new_file : bool , deleted_file : bool , copied_file : bool ,
283284raw_rename_from : Optional [bytes ], raw_rename_to : Optional [bytes ],
284- diff : Union [str , bytes , None ], change_type : Optional [str ], score : Optional [int ]) -> None :
285+ diff : Union [str , bytes , None ], change_type : Union [Lit_change_type , None ],
286+ score : Optional [int ]) -> None :
285287
286288assert a_rawpath is None or isinstance (a_rawpath , bytes )
287289assert b_rawpath is None or isinstance (b_rawpath , bytes )
@@ -498,12 +500,18 @@ def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: DiffIndex) -> Non
498500for line in lines .split (':' )[1 :]:
499501meta , _ , path = line .partition ('\x00 ' )
500502path = path .rstrip ('\x00 ' )
501- a_blob_id , b_blob_id = None , None # Type: Optional[str]
503+ a_blob_id : Union [str , None ]
504+ b_blob_id : Union [str , None ]
502505old_mode , new_mode , a_blob_id , b_blob_id , _change_type = meta .split (None , 4 )
503506# Change type can be R100
504507# R: status letter
505508# 100: score (in case of copy and rename)
506- change_type = _change_type [0 ]
509+
510+ def is_change_type (inp : str ) -> TypeGuard [Lit_change_type ]:
511+ return inp in Lit_change_type .__args__ # type: ignore
512+
513+ assert is_change_type (_change_type [0 ])
514+ change_type : Lit_change_type = _change_type [0 ]
507515score_str = '' .join (_change_type [1 :])
508516score = int (score_str ) if score_str .isdigit () else None
509517path = path .strip ()
@@ -518,7 +526,7 @@ def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: DiffIndex) -> Non
518526# NOTE: We cannot conclude from the existence of a blob to change type
519527# as diffs with the working do not have blobs yet
520528if change_type == 'D' :
521- b_blob_id = None # Optional[str]
529+ b_blob_id = None
522530deleted_file = True
523531elif change_type == 'A' :
524532a_blob_id = None
0 commit comments