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

Commitd9305d7

Browse files
Mypy types for completion (#928)
* Mypy types for completionWe're not checking these in CI nor are we providing a config file yet.I used mypy 0.910 in Python 3.6 run directly on bpython/autocomplete.py and still had 3 errors after these changes.
1 parent2308a61 commitd9305d7

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

‎bpython/autocomplete.py‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
importbuiltins
3434

3535
fromenumimportEnum
36-
fromtypingimportAny,Dict,Iterator,List,Match,NoReturn,Set,Union
36+
fromtypingimportAny,Dict,Iterator,List,Match,NoReturn,Set,Union,Tuple
3737
from .importinspection
3838
from .importlineaslineparts
3939
from .lineimportLinePart
@@ -180,7 +180,7 @@ def few_enough_underscores(current, match) -> bool:
180180
returnnotmatch.startswith("_")
181181

182182

183-
defmethod_match_none(word,size,text)->False:
183+
defmethod_match_none(word,size,text)->bool:
184184
returnFalse
185185

186186

@@ -214,7 +214,10 @@ def __init__(
214214
self._shown_before_tab=shown_before_tab
215215
self.method_match=MODES_MAP[mode]
216216

217-
defmatches(self,cursor_offset,line,**kwargs)->NoReturn:
217+
@abc.abstractmethod
218+
defmatches(
219+
self,cursor_offset:int,line:str,**kwargs
220+
)->Union[Set[str],None]:
218221
"""Returns a list of possible matches given a line and cursor, or None
219222
if this completion type isn't applicable.
220223
@@ -232,7 +235,8 @@ def matches(self, cursor_offset, line, **kwargs) -> NoReturn:
232235
"""
233236
raiseNotImplementedError
234237

235-
deflocate(self,cursor_offset,line)->NoReturn:
238+
@abc.abstractmethod
239+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
236240
"""Returns a Linepart namedtuple instance or None given cursor and line
237241
238242
A Linepart namedtuple contains a start, stop, and word. None is
@@ -243,9 +247,10 @@ def locate(self, cursor_offset, line) -> NoReturn:
243247
defformat(self,word):
244248
returnword
245249

246-
defsubstitute(self,cursor_offset,line,match)->NoReturn:
250+
defsubstitute(self,cursor_offset,line,match)->Tuple[int,str]:
247251
"""Returns a cursor offset and line with match swapped in"""
248252
lpart=self.locate(cursor_offset,line)
253+
assertlpart
249254
offset=lpart.start+len(match)
250255
changed_line=line[:lpart.start]+match+line[lpart.stop :]
251256
returnoffset,changed_line
@@ -269,16 +274,19 @@ def __init__(self, completers, mode=AutocompleteModes.SIMPLE) -> None:
269274

270275
super().__init__(True,mode)
271276

272-
deflocate(self,current_offset,line)->Union[None,NoReturn]:
277+
deflocate(self,current_offset:int,line:str)->Union[None,NoReturn]:
273278
forcompleterinself._completers:
274279
return_value=completer.locate(current_offset,line)
275280
ifreturn_valueisnotNone:
276281
returnreturn_value
282+
returnNone
277283

278284
defformat(self,word):
279285
returnself._completers[0].format(word)
280286

281-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
287+
defmatches(
288+
self,cursor_offset:int,line:str,**kwargs
289+
)->Union[None,Set]:
282290
return_value=None
283291
all_matches=set()
284292
forcompleterinself._completers:
@@ -344,7 +352,7 @@ class AttrCompletion(BaseCompletionType):
344352

345353
attr_matches_re=LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)")
346354

347-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Dict]:
355+
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
348356
if"locals_"notinkwargs:
349357
returnNone
350358
locals_=kwargs["locals_"]
@@ -427,15 +435,17 @@ def list_attributes(self, obj) -> List[str]:
427435

428436

429437
classDictKeyCompletion(BaseCompletionType):
430-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Dict]:
438+
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
431439
if"locals_"notinkwargs:
432440
returnNone
433441
locals_=kwargs["locals_"]
434442

435443
r=self.locate(cursor_offset,line)
436444
ifrisNone:
437445
returnNone
438-
_,_,dexpr=lineparts.current_dict(cursor_offset,line)
446+
curDictParts=lineparts.current_dict(cursor_offset,line)
447+
assertcurDictParts,"current_dict when .locate() truthy"
448+
_,_,dexpr=curDictParts
439449
try:
440450
obj=safe_eval(dexpr,locals_)
441451
exceptEvaluationError:
@@ -456,7 +466,7 @@ def format(self, match):
456466

457467

458468
classMagicMethodCompletion(BaseCompletionType):
459-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Dict]:
469+
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
460470
if"current_block"notinkwargs:
461471
returnNone
462472
current_block=kwargs["current_block"]
@@ -508,7 +518,7 @@ def locate(self, current_offset, line) -> Union[LinePart, None]:
508518

509519

510520
classParameterNameCompletion(BaseCompletionType):
511-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Dict]:
521+
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
512522
if"argspec"notinkwargs:
513523
returnNone
514524
argspec=kwargs["argspec"]
@@ -538,7 +548,7 @@ class ExpressionAttributeCompletion(AttrCompletion):
538548
deflocate(self,current_offset,line)->Union[LinePart,None]:
539549
returnlineparts.current_expression_attribute(current_offset,line)
540550

541-
defmatches(self,cursor_offset,line,**kwargs)->Union[Set,Dict,None]:
551+
defmatches(self,cursor_offset,line,**kwargs)->Union[Set,None]:
542552
if"locals_"notinkwargs:
543553
returnNone
544554
locals_=kwargs["locals_"]
@@ -547,6 +557,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, Dict, None]:
547557
locals_=__main__.__dict__
548558

549559
attr=self.locate(cursor_offset,line)
560+
assertattr,"locate was already truthy for the same call"
550561

551562
try:
552563
obj=evaluate_current_expression(cursor_offset,line,locals_)
@@ -570,7 +581,9 @@ def matches(self, cursor_offset, line, **kwargs) -> None:
570581
else:
571582

572583
classJediCompletion(BaseCompletionType):
573-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Dict]:
584+
_orig_start:Union[int,None]
585+
586+
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
574587
if"history"notinkwargs:
575588
returnNone
576589
history=kwargs["history"]
@@ -596,6 +609,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
596609
else:
597610
self._orig_start=None
598611
returnNone
612+
assertisinstance(self._orig_start,int)
599613

600614
first_letter=line[self._orig_start :self._orig_start+1]
601615

@@ -610,13 +624,14 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
610624
# case-sensitive matches only
611625
return {mforminmatchesifm.startswith(first_letter)}
612626

613-
deflocate(self,cursor_offset,line)->LinePart:
627+
deflocate(self,cursor_offset:int,line:str)->LinePart:
628+
assertisinstance(self._orig_start,int)
614629
start=self._orig_start
615630
end=cursor_offset
616631
returnLinePart(start,end,line[start:end])
617632

618633
classMultilineJediCompletion(JediCompletion):
619-
defmatches(self,cursor_offset,line,**kwargs)->Union[Dict,None]:
634+
defmatches(self,cursor_offset,line,**kwargs)->Union[Set,None]:
620635
if"current_block"notinkwargsor"history"notinkwargs:
621636
returnNone
622637
current_block=kwargs["current_block"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp