4242
4343# typing ---------------------------------------------------------------------------
4444
45- from typing import TYPE_CHECKING
45+ from typing import Any , BinaryIO , Callable , Dict , Mapping , Sequence , TYPE_CHECKING , Union
4646
47- from git .types import TBD
47+ from git .types import PathLike , TBD
4848
4949if TYPE_CHECKING :
50- pass
50+ pass
5151
5252
5353# ---------------------------------------------------------------------------------
6969# Documentation
7070## @{
7171
72- def handle_process_output (process , stdout_handler , stderr_handler ,
73- finalizer = None , decode_streams = True ):
72+ def handle_process_output (process : subprocess .Popen ,
73+ stdout_handler : Union [None , Callable [[str ], None ]],
74+ stderr_handler : Union [None , Callable [[str ], None ]],
75+ finalizer : Union [None , Callable [[subprocess .Popen ], TBD ]] = None ,
76+ decode_streams : bool = True ) -> Union [None , TBD ]: # TBD is whatever finalizer returns
7477 """Registers for notifications to learn that process output is ready to read, and dispatches lines to
7578 the respective line handlers.
7679 This function returns once the finalizer returns
@@ -87,13 +90,14 @@ def handle_process_output(process, stdout_handler, stderr_handler,
8790 or if decoding must happen later (i.e. for Diffs).
8891 """
8992 # Use 2 "pump" threads and wait for both to finish.
90- def pump_stream (cmdline , name , stream , is_decode , handler ):
93+ def pump_stream (cmdline : str , name : str , stream : BinaryIO , is_decode : bool ,
94+ handler : Union [None , Callable [[str ], None ]]) -> None :
9195 try :
9296 for line in stream :
9397 if handler :
9498 if is_decode :
95- line = line .decode (defenc )
96- handler (line )
99+ line_str = line .decode (defenc )
100+ handler (line_str )
97101 except Exception as ex :
98102 log .error ("Pumping %r of cmd(%s) failed due to: %r" , name , remove_password_if_present (cmdline ), ex )
99103 raise CommandError (['<%s-pump>' % name ] + remove_password_if_present (cmdline ), ex ) from ex
@@ -126,17 +130,20 @@ def pump_stream(cmdline, name, stream, is_decode, handler):
126130
127131 if finalizer :
128132 return finalizer (process )
133+ else :
134+ return None
129135
130136
131- def dashify (string ) :
137+ def dashify (string : str ) -> str :
132138 return string .replace ('_' , '-' )
133139
134140
135- def slots_to_dict (self , exclude = ()):
141+ def slots_to_dict (self , exclude : Sequence [str ] = ()) -> Dict [str , Any ]:
142+ # annotate self.__slots__ as Tuple[str, ...] once 3.5 dropped
136143 return {s : getattr (self , s ) for s in self .__slots__ if s not in exclude }
137144
138145
139- def dict_to_slots_and__excluded_are_none (self , d , excluded = ()):
146+ def dict_to_slots_and__excluded_are_none (self , d : Mapping [ str , Any ], excluded : Sequence [ str ] = ()) -> None :
140147 for k , v in d .items ():
141148 setattr (self , k , v )
142149 for k in excluded :
@@ -175,10 +182,10 @@ class Git(LazyMixin):
175182
176183 _excluded_ = ('cat_file_all' , 'cat_file_header' , '_version_info' )
177184
178- def __getstate__ (self ):
185+ def __getstate__ (self ) -> Dict [ str , Any ] :
179186 return slots_to_dict (self , exclude = self ._excluded_ )
180187
181- def __setstate__ (self , d ):
188+ def __setstate__ (self , d ) -> None :
182189 dict_to_slots_and__excluded_are_none (self , d , excluded = self ._excluded_ )
183190
184191 # CONFIGURATION
@@ -202,7 +209,7 @@ def __setstate__(self, d):
202209 # the top level __init__
203210
204211 @classmethod
205- def refresh (cls , path = None ):
212+ def refresh (cls , path : Union [ None , PathLike ] = None ) -> bool :
206213 """This gets called by the refresh function (see the top level
207214 __init__).
208215 """
@@ -317,11 +324,11 @@ def refresh(cls, path=None):
317324 return has_git
318325
319326 @classmethod
320- def is_cygwin (cls ):
327+ def is_cygwin (cls ) -> bool :
321328 return is_cygwin_git (cls .GIT_PYTHON_GIT_EXECUTABLE )
322329
323330 @classmethod
324- def polish_url (cls , url , is_cygwin = None ):
331+ def polish_url (cls , url : str , is_cygwin : Union [ None , bool ] = None ) -> str :
325332 if is_cygwin is None :
326333 is_cygwin = cls .is_cygwin ()
327334
@@ -338,7 +345,6 @@ def polish_url(cls, url, is_cygwin=None):
338345 if url .startswith ('~' ):
339346 url = os .path .expanduser (url )
340347 url = url .replace ("\\ \\ " , "\\ " ).replace ("\\ " , "/" )
341-
342348 return url
343349
344350 class AutoInterrupt (object ):
0 commit comments