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

Commitfe594eb

Browse files
committed
Add T_Tre_cache TypeVar
1 parentaffee35 commitfe594eb

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

‎git/objects/tree.py‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# typing -------------------------------------------------
2222

23-
fromtypingimportCallable,Dict,Iterable,Iterator,List,Tuple,Type,Union,cast,TYPE_CHECKING
23+
fromtypingimportCallable,Dict,Generic,Iterable,Iterator,List,Tuple,Type,TypeVar,Union,cast,TYPE_CHECKING
2424

2525
fromgit.typesimportPathLike
2626

@@ -31,13 +31,16 @@
3131
#--------------------------------------------------------
3232

3333

34-
cmp:Callable[[int,int],int]=lambdaa,b: (a>b)- (a<b)
34+
cmp:Callable[[str,str],int]=lambdaa,b: (a>b)- (a<b)
3535

3636
__all__= ("TreeModifier","Tree")
3737

38+
T_Tree_cache=TypeVar('T_Tree_cache',bound=Union[Tuple[bytes,int,str]])
3839

39-
defgit_cmp(t1:'Tree',t2:'Tree')->int:
40+
41+
defgit_cmp(t1:T_Tree_cache,t2:T_Tree_cache)->int:
4042
a,b=t1[2],t2[2]
43+
assertisinstance(a,str)andisinstance(b,str)# Need as mypy 9.0 cannot unpack TypeVar properly
4144
len_a,len_b=len(a),len(b)
4245
min_len=min(len_a,len_b)
4346
min_cmp=cmp(a[:min_len],b[:min_len])
@@ -48,7 +51,8 @@ def git_cmp(t1: 'Tree', t2: 'Tree') -> int:
4851
returnlen_a-len_b
4952

5053

51-
defmerge_sort(a:List[int],cmp:Callable[[int,int],int])->None:
54+
defmerge_sort(a:List[T_Tree_cache],
55+
cmp:Callable[[T_Tree_cache,T_Tree_cache],int])->None:
5256
iflen(a)<2:
5357
returnNone
5458

@@ -83,18 +87,18 @@ def merge_sort(a: List[int], cmp: Callable[[int, int], int]) -> None:
8387
k=k+1
8488

8589

86-
classTreeModifier(object):
90+
classTreeModifier(Generic[T_Tree_cache],object):
8791

8892
"""A utility class providing methods to alter the underlying cache in a list-like fashion.
8993
9094
Once all adjustments are complete, the _cache, which really is a reference to
9195
the cache of a tree, will be sorted. Assuring it will be in a serializable state"""
9296
__slots__='_cache'
9397

94-
def__init__(self,cache):
98+
def__init__(self,cache:List[T_Tree_cache])->None:
9599
self._cache=cache
96100

97-
def_index_by_name(self,name):
101+
def_index_by_name(self,name:str)->int:
98102
""":return: index of an item with name, or -1 if not found"""
99103
fori,tinenumerate(self._cache):
100104
ift[2]==name:
@@ -104,7 +108,7 @@ def _index_by_name(self, name):
104108
return-1
105109

106110
#{ Interface
107-
defset_done(self):
111+
defset_done(self)->'TreeModifier':
108112
"""Call this method once you are done modifying the tree information.
109113
It may be called several times, but be aware that each call will cause
110114
a sort operation
@@ -114,7 +118,7 @@ def set_done(self):
114118
#} END interface
115119

116120
#{ Mutators
117-
defadd(self,sha,mode,name,force=False):
121+
defadd(self,sha:bytes,mode:int,name:str,force:bool=False)->'TreeModifier':
118122
"""Add the given item to the tree. If an item with the given name already
119123
exists, nothing will be done, but a ValueError will be raised if the
120124
sha and mode of the existing item do not match the one you add, unless
@@ -132,7 +136,7 @@ def add(self, sha, mode, name, force=False):
132136

133137
sha=to_bin_sha(sha)
134138
index=self._index_by_name(name)
135-
item= (sha,mode,name)
139+
item:T_Tree_cache= (sha,mode,name)# type: ignore ## use Typeguard from typing-extensions 3.10.0
136140
ifindex==-1:
137141
self._cache.append(item)
138142
else:
@@ -195,7 +199,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
195199
def__init__(self,repo:'Repo',binsha:bytes,mode:int=tree_id<<12,path:Union[PathLike,None]=None):
196200
super(Tree,self).__init__(repo,binsha,mode,path)
197201

198-
@classmethod
202+
@classmethod
199203
def_get_intermediate_items(cls,index_object:'Tree',# type: ignore
200204
)->Union[Tuple['Tree', ...],Tuple[()]]:
201205
ifindex_object.type=="tree":
@@ -261,17 +265,17 @@ def __truediv__(self, file: str) -> Union['Tree', Blob, Submodule]:
261265
"""For PY3 only"""
262266
returnself.join(file)
263267

264-
@property
268+
@property
265269
deftrees(self)->List['Tree']:
266270
""":return: list(Tree, ...) list of trees directly below this tree"""
267271
return [iforiinselfifi.type=="tree"]
268272

269-
@property
273+
@property
270274
defblobs(self)->List['Blob']:
271275
""":return: list(Blob, ...) list of blobs directly below this tree"""
272276
return [iforiinselfifi.type=="blob"]
273277

274-
@property
278+
@property
275279
defcache(self)->TreeModifier:
276280
"""
277281
:return: An object allowing to modify the internal cache. This can be used

‎git/types.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ class Total_TD(TypedDict):
3939

4040
classHSH_TD(TypedDict):
4141
total:Total_TD
42-
files:Dict[str,Files_TD]
42+
files:Dict[PathLike,Files_TD]

‎git/util.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def _from_string(cls, string: str) -> 'Actor':
672672

673673
@classmethod
674674
def_main_actor(cls,env_name:str,env_email:str,
675-
config_reader:Union[None,GitConfigParser,SectionConstraint]=None)->'Actor':
675+
config_reader:Union[None,'GitConfigParser','SectionConstraint']=None)->'Actor':
676676
actor=Actor('','')
677677
user_id=None# We use this to avoid multiple calls to getpass.getuser()
678678

@@ -701,7 +701,7 @@ def default_name() -> str:
701701
returnactor
702702

703703
@classmethod
704-
defcommitter(cls,config_reader:Union[None,GitConfigParser,SectionConstraint]=None)->'Actor':
704+
defcommitter(cls,config_reader:Union[None,'GitConfigParser','SectionConstraint']=None)->'Actor':
705705
"""
706706
:return: Actor instance corresponding to the configured committer. It behaves
707707
similar to the git implementation, such that the environment will override
@@ -748,7 +748,7 @@ class Stats(object):
748748

749749
fromgit.typesimportTotal_TD,Files_TD
750750

751-
def__init__(self,total:Total_TD,files:Dict[str,Files_TD]):
751+
def__init__(self,total:Total_TD,files:Dict[PathLike,Files_TD]):
752752
self.total=total
753753
self.files=files
754754

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp