Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit887f249

Browse files
committed
Add types to cmd.py Git
1 parent3473060 commit887f249

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

‎git/cmd.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242

4343
# typing ---------------------------------------------------------------------------
4444

45-
fromtypingimportTYPE_CHECKING
45+
fromtypingimportAny,BinaryIO,Callable,Dict,Mapping,Sequence,TYPE_CHECKING,Union
4646

47-
fromgit.typesimportTBD
47+
fromgit.typesimportPathLike,TBD
4848

4949
ifTYPE_CHECKING:
50-
pass
50+
pass
5151

5252

5353
# ---------------------------------------------------------------------------------
@@ -69,8 +69,11 @@
6969
# Documentation
7070
## @{
7171

72-
defhandle_process_output(process,stdout_handler,stderr_handler,
73-
finalizer=None,decode_streams=True):
72+
defhandle_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-
defpump_stream(cmdline,name,stream,is_decode,handler):
93+
defpump_stream(cmdline:str,name:str,stream:BinaryIO,is_decode:bool,
94+
handler:Union[None,Callable[[str],None]])->None:
9195
try:
9296
forlineinstream:
9397
ifhandler:
9498
ifis_decode:
95-
line=line.decode(defenc)
96-
handler(line)
99+
line_str=line.decode(defenc)
100+
handler(line_str)
97101
exceptExceptionasex:
98102
log.error("Pumping %r of cmd(%s) failed due to: %r",name,remove_password_if_present(cmdline),ex)
99103
raiseCommandError(['<%s-pump>'%name]+remove_password_if_present(cmdline),ex)fromex
@@ -126,17 +130,20 @@ def pump_stream(cmdline, name, stream, is_decode, handler):
126130

127131
iffinalizer:
128132
returnfinalizer(process)
133+
else:
134+
returnNone
129135

130136

131-
defdashify(string):
137+
defdashify(string:str)->str:
132138
returnstring.replace('_','-')
133139

134140

135-
defslots_to_dict(self,exclude=()):
141+
defslots_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)forsinself.__slots__ifsnotinexclude}
137144

138145

139-
defdict_to_slots_and__excluded_are_none(self,d,excluded=()):
146+
defdict_to_slots_and__excluded_are_none(self,d:Mapping[str,Any],excluded:Sequence[str]=())->None:
140147
fork,vind.items():
141148
setattr(self,k,v)
142149
forkinexcluded:
@@ -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
returnslots_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-
defrefresh(cls,path=None):
212+
defrefresh(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
returnhas_git
318325

319326
@classmethod
320-
defis_cygwin(cls):
327+
defis_cygwin(cls)->bool:
321328
returnis_cygwin_git(cls.GIT_PYTHON_GIT_EXECUTABLE)
322329

323330
@classmethod
324-
defpolish_url(cls,url,is_cygwin=None):
331+
defpolish_url(cls,url:str,is_cygwin:Union[None,bool]=None)->str:
325332
ifis_cygwinisNone:
326333
is_cygwin=cls.is_cygwin()
327334

@@ -338,7 +345,6 @@ def polish_url(cls, url, is_cygwin=None):
338345
ifurl.startswith('~'):
339346
url=os.path.expanduser(url)
340347
url=url.replace("\\\\","\\").replace("\\","/")
341-
342348
returnurl
343349

344350
classAutoInterrupt(object):

‎git/util.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
# typing ---------------------------------------------------------
2323

2424
fromtypingimport (Any,AnyStr,BinaryIO,Callable,Dict,Generator,IO,Iterator,List,
25-
Optional,Pattern,Sequence,Tuple,Union,cast,TYPE_CHECKING)
25+
Optional,Pattern,Sequence,Tuple,Union,cast,TYPE_CHECKING,overload)
26+
27+
2628
ifTYPE_CHECKING:
2729
fromgit.remoteimportRemote
2830
fromgit.repo.baseimportRepo
29-
from .typesimportPathLike,TBD
31+
from .typesimportPathLike,TBD,Literal
3032

3133
# ---------------------------------------------------------------------
3234

@@ -279,9 +281,20 @@ def _cygexpath(drive: Optional[str], path: PathLike) -> str:
279281
)# type: Tuple[Tuple[Pattern[str], Callable, bool], ...]
280282

281283

284+
@overload
285+
defcygpath(path:str)->str:
286+
...
287+
288+
289+
@overload
290+
defcygpath(path:PathLike)->PathLike:
291+
...
292+
293+
282294
defcygpath(path:PathLike)->PathLike:
283295
"""Use :meth:`git.cmd.Git.polish_url()` instead, that works on any environment."""
284-
path=str(path)# ensure is str and not AnyPath
296+
path=str(path)# ensure is str and not AnyPath.
297+
#Fix to use Paths when 3.5 dropped. or to be just str if only for urls?
285298
ifnotpath.startswith(('/cygdrive','//')):
286299
forregex,parser,recursein_cygpath_parsers:
287300
match=regex.match(path)
@@ -314,10 +327,23 @@ def decygpath(path: PathLike) -> str:
314327
_is_cygwin_cache= {}# type: Dict[str, Optional[bool]]
315328

316329

330+
@overload
331+
defis_cygwin_git(git_executable:None)->Literal[False]:
332+
...
333+
334+
335+
@overload
317336
defis_cygwin_git(git_executable:PathLike)->bool:
337+
...
338+
339+
340+
defis_cygwin_git(git_executable:Union[None,PathLike])->bool:
318341
ifnotis_win:
319342
returnFalse
320343

344+
ifgit_executableisNone:
345+
returnFalse# or raise error?
346+
321347
#from subprocess import check_output
322348
git_executable=str(git_executable)
323349
is_cygwin=_is_cygwin_cache.get(git_executable)# type: Optional[bool]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp