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

Commit55e482a

Browse files
disallow untyped defs in autocomplete.py
1 parent3356afc commit55e482a

File tree

1 file changed

+99
-52
lines changed

1 file changed

+99
-52
lines changed

‎bpython/autocomplete.py‎

Lines changed: 99 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
# THE SOFTWARE.
2323

24+
# To gradually migrate to mypy we aren't setting these globally yet
25+
# mypy: disallow_untyped_defs=True
2426

2527
import__main__
2628
importabc
@@ -35,10 +37,12 @@
3537
fromenumimportEnum
3638
fromtypingimport (
3739
Any,
40+
cast,
3841
Dict,
3942
Iterator,
4043
List,
4144
Match,
45+
Optional,
4246
Set,
4347
Union,
4448
Tuple,
@@ -50,6 +54,7 @@
5054
from .lineimportLinePart
5155
from .lazyreimportLazyReCompile
5256
from .simpleevalimportsafe_eval,evaluate_current_expression,EvaluationError
57+
from .importcompletionimportModuleGatherer
5358

5459

5560
# Autocomplete modes
@@ -60,7 +65,7 @@ class AutocompleteModes(Enum):
6065
FUZZY="fuzzy"
6166

6267
@classmethod
63-
deffrom_string(cls,value)->Union[Any,None]:
68+
deffrom_string(cls,value:str)->Union[Any,None]:
6469
ifvalue.upper()incls.__members__:
6570
returncls.__members__[value.upper()]
6671
returnNone
@@ -177,7 +182,7 @@ def after_last_dot(name: str) -> str:
177182
returnname.rstrip(".").rsplit(".")[-1]
178183

179184

180-
deffew_enough_underscores(current,match)->bool:
185+
deffew_enough_underscores(current:str,match:str)->bool:
181186
"""Returns whether match should be shown based on current
182187
183188
if current is _, True if match starts with 0 or 1 underscore
@@ -203,7 +208,7 @@ def method_match_substring(word: str, size: int, text: str) -> bool:
203208
returntextinword
204209

205210

206-
defmethod_match_fuzzy(word,size,text)->Union[Match,None]:
211+
defmethod_match_fuzzy(word:str,size:int,text:str)->Union[Match,None]:
207212
s=r".*%s.*"%".*".join(list(text))
208213
returnre.search(s,word)
209214

@@ -220,14 +225,16 @@ class BaseCompletionType:
220225
"""Describes different completion types"""
221226

222227
def__init__(
223-
self,shown_before_tab:bool=True,mode=AutocompleteModes.SIMPLE
228+
self,
229+
shown_before_tab:bool=True,
230+
mode:AutocompleteModes=AutocompleteModes.SIMPLE,
224231
)->None:
225232
self._shown_before_tab=shown_before_tab
226233
self.method_match=MODES_MAP[mode]
227234

228235
@abc.abstractmethod
229236
defmatches(
230-
self,cursor_offset:int,line:str,**kwargs
237+
self,cursor_offset:int,line:str,**kwargs:Any
231238
)->Union[Set[str],None]:
232239
"""Returns a list of possible matches given a line and cursor, or None
233240
if this completion type isn't applicable.
@@ -255,10 +262,12 @@ def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
255262
the cursor."""
256263
raiseNotImplementedError
257264

258-
defformat(self,word):
265+
defformat(self,word:str)->str:
259266
returnword
260267

261-
defsubstitute(self,cursor_offset,line,match)->Tuple[int,str]:
268+
defsubstitute(
269+
self,cursor_offset:int,line:str,match:str
270+
)->Tuple[int,str]:
262271
"""Returns a cursor offset and line with match swapped in"""
263272
lpart=self.locate(cursor_offset,line)
264273
assertlpart
@@ -289,18 +298,18 @@ def __init__(
289298

290299
super().__init__(True,mode)
291300

292-
deflocate(self,current_offset,line):
301+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
293302
forcompleterinself._completers:
294-
return_value=completer.locate(current_offset,line)
303+
return_value=completer.locate(cursor_offset,line)
295304
ifreturn_valueisnotNone:
296305
returnreturn_value
297306
returnNone
298307

299-
defformat(self,word):
308+
defformat(self,word:str)->str:
300309
returnself._completers[0].format(word)
301310

302311
defmatches(
303-
self,cursor_offset:int,line:str,**kwargs
312+
self,cursor_offset:int,line:str,**kwargs:Any
304313
)->Union[None,Set]:
305314
return_value=None
306315
all_matches=set()
@@ -316,28 +325,36 @@ def matches(
316325

317326

318327
classImportCompletion(BaseCompletionType):
319-
def__init__(self,module_gatherer,mode=AutocompleteModes.SIMPLE):
328+
def__init__(
329+
self,
330+
module_gatherer:ModuleGatherer,
331+
mode:AutocompleteModes=AutocompleteModes.SIMPLE,
332+
):
320333
super().__init__(False,mode)
321334
self.module_gatherer=module_gatherer
322335

323-
defmatches(self,cursor_offset,line,**kwargs):
336+
defmatches(
337+
self,cursor_offset:int,line:str,**kwargs:Any
338+
)->Union[None,Set]:
324339
returnself.module_gatherer.complete(cursor_offset,line)
325340

326-
deflocate(self,current_offset,line):
327-
returnlineparts.current_word(current_offset,line)
341+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
342+
returnlineparts.current_word(cursor_offset,line)
328343

329-
defformat(self,word):
344+
defformat(self,word:str)->str:
330345
returnafter_last_dot(word)
331346

332347

333348
classFilenameCompletion(BaseCompletionType):
334-
def__init__(self,mode=AutocompleteModes.SIMPLE):
349+
def__init__(self,mode:AutocompleteModes=AutocompleteModes.SIMPLE):
335350
super().__init__(False,mode)
336351

337-
defsafe_glob(self,pathname)->Iterator:
352+
defsafe_glob(self,pathname:str)->Iterator[str]:
338353
returnglob.iglob(glob.escape(pathname)+"*")
339354

340-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,set]:
355+
defmatches(
356+
self,cursor_offset:int,line:str,**kwargs:Any
357+
)->Union[None,Set]:
341358
cs=lineparts.current_string(cursor_offset,line)
342359
ifcsisNone:
343360
returnNone
@@ -352,10 +369,10 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, set]:
352369
matches.add(filename)
353370
returnmatches
354371

355-
deflocate(self,current_offset,line):
356-
returnlineparts.current_string(current_offset,line)
372+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
373+
returnlineparts.current_string(cursor_offset,line)
357374

358-
defformat(self,filename):
375+
defformat(self,filename:str)->str:
359376
filename.rstrip(os.sep).rsplit(os.sep)[-1]
360377
ifos.sepinfilename[:-1]:
361378
returnfilename[filename.rindex(os.sep,0,-1)+1 :]
@@ -367,10 +384,12 @@ class AttrCompletion(BaseCompletionType):
367384

368385
attr_matches_re=LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)")
369386

370-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
387+
defmatches(
388+
self,cursor_offset:int,line:str,**kwargs:Any
389+
)->Union[None,Set]:
371390
if"locals_"notinkwargs:
372391
returnNone
373-
locals_=kwargs["locals_"]
392+
locals_=cast(Dict[str,Any],kwargs["locals_"])
374393

375394
r=self.locate(cursor_offset,line)
376395
ifrisNone:
@@ -397,13 +416,13 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
397416
iffew_enough_underscores(r.word.split(".")[-1],m.split(".")[-1])
398417
}
399418

400-
deflocate(self,current_offset,line):
401-
returnlineparts.current_dotted_attribute(current_offset,line)
419+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
420+
returnlineparts.current_dotted_attribute(cursor_offset,line)
402421

403-
defformat(self,word):
422+
defformat(self,word:str)->str:
404423
returnafter_last_dot(word)
405424

406-
defattr_matches(self,text,namespace)->List:
425+
defattr_matches(self,text:str,namespace:Dict[str,Any])->List:
407426
"""Taken from rlcompleter.py and bent to my will."""
408427

409428
m=self.attr_matches_re.match(text)
@@ -422,7 +441,7 @@ def attr_matches(self, text, namespace) -> List:
422441
matches=self.attr_lookup(obj,expr,attr)
423442
returnmatches
424443

425-
defattr_lookup(self,obj,expr,attr)->List:
444+
defattr_lookup(self,obj:Any,expr:str,attr:str)->List:
426445
"""Second half of attr_matches."""
427446
words=self.list_attributes(obj)
428447
ifinspection.hasattr_safe(obj,"__class__"):
@@ -442,15 +461,17 @@ def attr_lookup(self, obj, expr, attr) -> List:
442461
matches.append(f"{expr}.{word}")
443462
returnmatches
444463

445-
deflist_attributes(self,obj)->List[str]:
464+
deflist_attributes(self,obj:Any)->List[str]:
446465
# TODO: re-implement dir using getattr_static to avoid using
447466
# AttrCleaner here?
448467
withinspection.AttrCleaner(obj):
449468
returndir(obj)
450469

451470

452471
classDictKeyCompletion(BaseCompletionType):
453-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
472+
defmatches(
473+
self,cursor_offset:int,line:str,**kwargs:Any
474+
)->Union[None,Set]:
454475
if"locals_"notinkwargs:
455476
returnNone
456477
locals_=kwargs["locals_"]
@@ -473,15 +494,17 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
473494
else:
474495
returnNone
475496

476-
deflocate(self,current_offset,line)->Union[LinePart,None]:
477-
returnlineparts.current_dict_key(current_offset,line)
497+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
498+
returnlineparts.current_dict_key(cursor_offset,line)
478499

479-
defformat(self,match):
500+
defformat(self,match:str)->str:
480501
returnmatch[:-1]
481502

482503

483504
classMagicMethodCompletion(BaseCompletionType):
484-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
505+
defmatches(
506+
self,cursor_offset:int,line:str,**kwargs:Any
507+
)->Union[None,Set]:
485508
if"current_block"notinkwargs:
486509
returnNone
487510
current_block=kwargs["current_block"]
@@ -493,12 +516,14 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
493516
returnNone
494517
return {namefornameinMAGIC_METHODSifname.startswith(r.word)}
495518

496-
deflocate(self,current_offset,line)->Union[LinePart,None]:
497-
returnlineparts.current_method_definition_name(current_offset,line)
519+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
520+
returnlineparts.current_method_definition_name(cursor_offset,line)
498521

499522

500523
classGlobalCompletion(BaseCompletionType):
501-
defmatches(self,cursor_offset,line,**kwargs)->Union[Set,None]:
524+
defmatches(
525+
self,cursor_offset:int,line:str,**kwargs:Any
526+
)->Union[None,Set]:
502527
"""Compute matches when text is a simple name.
503528
Return a list of all keywords, built-in functions and names currently
504529
defined in self.namespace that match.
@@ -528,12 +553,14 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, None]:
528553
matches.add(_callable_postfix(val,word))
529554
returnmatchesifmatcheselseNone
530555

531-
deflocate(self,current_offset,line)->Union[LinePart,None]:
532-
returnlineparts.current_single_word(current_offset,line)
556+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
557+
returnlineparts.current_single_word(cursor_offset,line)
533558

534559

535560
classParameterNameCompletion(BaseCompletionType):
536-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
561+
defmatches(
562+
self,cursor_offset:int,line:str,**kwargs:Any
563+
)->Union[None,Set]:
537564
if"argspec"notinkwargs:
538565
returnNone
539566
argspec=kwargs["argspec"]
@@ -554,16 +581,18 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
554581
)
555582
returnmatchesifmatcheselseNone
556583

557-
deflocate(self,current_offset,line)->Union[LinePart,None]:
558-
returnlineparts.current_word(current_offset,line)
584+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
585+
returnlineparts.current_word(cursor_offset,line)
559586

560587

561588
classExpressionAttributeCompletion(AttrCompletion):
562589
# could replace attr completion as a more general case with some work
563-
deflocate(self,current_offset,line)->Union[LinePart,None]:
564-
returnlineparts.current_expression_attribute(current_offset,line)
590+
deflocate(self,cursor_offset:int,line:str)->Union[LinePart,None]:
591+
returnlineparts.current_expression_attribute(cursor_offset,line)
565592

566-
defmatches(self,cursor_offset,line,**kwargs)->Union[Set,None]:
593+
defmatches(
594+
self,cursor_offset:int,line:str,**kwargs:Any
595+
)->Union[None,Set]:
567596
if"locals_"notinkwargs:
568597
returnNone
569598
locals_=kwargs["locals_"]
@@ -589,7 +618,14 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, None]:
589618
exceptImportError:
590619

591620
classMultilineJediCompletion(BaseCompletionType):# type: ignore [no-redef]
592-
defmatches(self,cursor_offset,line,**kwargs)->None:
621+
defmatches(
622+
self,cursor_offset:int,line:str,**kwargs:Any
623+
)->Union[None,Set]:
624+
returnNone
625+
626+
deflocate(
627+
self,cursor_offset:int,line:str
628+
)->Union[LinePart,None]:
593629
returnNone
594630

595631

@@ -598,7 +634,9 @@ def matches(self, cursor_offset, line, **kwargs) -> None:
598634
classJediCompletion(BaseCompletionType):
599635
_orig_start:Union[int,None]
600636

601-
defmatches(self,cursor_offset,line,**kwargs)->Union[None,Set]:
637+
defmatches(
638+
self,cursor_offset:int,line:str,**kwargs:Any
639+
)->Union[None,Set]:
602640
if"history"notinkwargs:
603641
returnNone
604642
history=kwargs["history"]
@@ -646,7 +684,9 @@ def locate(self, cursor_offset: int, line: str) -> LinePart:
646684
returnLinePart(start,end,line[start:end])
647685

648686
classMultilineJediCompletion(JediCompletion):# type: ignore [no-redef]
649-
defmatches(self,cursor_offset,line,**kwargs)->Union[Set,None]:
687+
defmatches(
688+
self,cursor_offset:int,line:str,**kwargs:Any
689+
)->Union[None,Set]:
650690
if"current_block"notinkwargsor"history"notinkwargs:
651691
returnNone
652692
current_block=kwargs["current_block"]
@@ -663,7 +703,12 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, None]:
663703
returnNone
664704

665705

666-
defget_completer(completers,cursor_offset,line,**kwargs):
706+
defget_completer(
707+
completers:Sequence[BaseCompletionType],
708+
cursor_offset:int,
709+
line:str,
710+
**kwargs:Any,
711+
)->Tuple[List[str],Optional[BaseCompletionType]]:
667712
"""Returns a list of matches and an applicable completer
668713
669714
If no matches available, returns a tuple of an empty list and None
@@ -698,7 +743,9 @@ def get_completer(completers, cursor_offset, line, **kwargs):
698743
return [],None
699744

700745

701-
defget_default_completer(mode=AutocompleteModes.SIMPLE,module_gatherer=None):
746+
defget_default_completer(
747+
mode:AutocompleteModes,module_gatherer:ModuleGatherer
748+
)->Tuple[BaseCompletionType, ...]:
702749
return (
703750
(
704751
DictKeyCompletion(mode=mode),
@@ -721,7 +768,7 @@ def get_default_completer(mode=AutocompleteModes.SIMPLE, module_gatherer=None):
721768
)
722769

723770

724-
def_callable_postfix(value,word):
771+
def_callable_postfix(value:Any,word:str)->str:
725772
"""rlcompleter's _callable_postfix done right."""
726773
ifcallable(value):
727774
word+="("

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp