33
33
import builtins
34
34
35
35
from enum import Enum
36
- from typing import Any , Dict , Iterator , List , Match , NoReturn , Set , Union
36
+ from typing import Any , Dict , Iterator , List , Match , NoReturn , Set , Union , Tuple
37
37
from . import inspection
38
38
from . import line as lineparts
39
39
from .line import LinePart
@@ -180,7 +180,7 @@ def few_enough_underscores(current, match) -> bool:
180
180
return not match .startswith ("_" )
181
181
182
182
183
- def method_match_none (word , size , text ) -> False :
183
+ def method_match_none (word , size , text ) -> bool :
184
184
return False
185
185
186
186
@@ -214,7 +214,10 @@ def __init__(
214
214
self ._shown_before_tab = shown_before_tab
215
215
self .method_match = MODES_MAP [mode ]
216
216
217
- def matches (self , cursor_offset , line , ** kwargs ) -> NoReturn :
217
+ @abc .abstractmethod
218
+ def matches (
219
+ self , cursor_offset : int , line : str , ** kwargs
220
+ ) -> Union [Set [str ], None ]:
218
221
"""Returns a list of possible matches given a line and cursor, or None
219
222
if this completion type isn't applicable.
220
223
@@ -232,7 +235,8 @@ def matches(self, cursor_offset, line, **kwargs) -> NoReturn:
232
235
"""
233
236
raise NotImplementedError
234
237
235
- def locate (self , cursor_offset , line ) -> NoReturn :
238
+ @abc .abstractmethod
239
+ def locate (self , cursor_offset : int , line : str ) -> Union [LinePart , None ]:
236
240
"""Returns a Linepart namedtuple instance or None given cursor and line
237
241
238
242
A Linepart namedtuple contains a start, stop, and word. None is
@@ -243,9 +247,10 @@ def locate(self, cursor_offset, line) -> NoReturn:
243
247
def format (self , word ):
244
248
return word
245
249
246
- def substitute (self , cursor_offset , line , match ) -> NoReturn :
250
+ def substitute (self , cursor_offset , line , match ) -> Tuple [ int , str ] :
247
251
"""Returns a cursor offset and line with match swapped in"""
248
252
lpart = self .locate (cursor_offset , line )
253
+ assert lpart
249
254
offset = lpart .start + len (match )
250
255
changed_line = line [: lpart .start ] + match + line [lpart .stop :]
251
256
return offset , changed_line
@@ -269,16 +274,19 @@ def __init__(self, completers, mode=AutocompleteModes.SIMPLE) -> None:
269
274
270
275
super ().__init__ (True , mode )
271
276
272
- def locate (self , current_offset , line ) -> Union [None , NoReturn ]:
277
+ def locate (self , current_offset : int , line : str ) -> Union [None , NoReturn ]:
273
278
for completer in self ._completers :
274
279
return_value = completer .locate (current_offset , line )
275
280
if return_value is not None :
276
281
return return_value
282
+ return None
277
283
278
284
def format (self , word ):
279
285
return self ._completers [0 ].format (word )
280
286
281
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Set ]:
287
+ def matches (
288
+ self , cursor_offset : int , line : str , ** kwargs
289
+ ) -> Union [None , Set ]:
282
290
return_value = None
283
291
all_matches = set ()
284
292
for completer in self ._completers :
@@ -344,7 +352,7 @@ class AttrCompletion(BaseCompletionType):
344
352
345
353
attr_matches_re = LazyReCompile (r"(\w+(\.\w+)*)\.(\w*)" )
346
354
347
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Dict ]:
355
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Set ]:
348
356
if "locals_" not in kwargs :
349
357
return None
350
358
locals_ = kwargs ["locals_" ]
@@ -427,15 +435,17 @@ def list_attributes(self, obj) -> List[str]:
427
435
428
436
429
437
class DictKeyCompletion (BaseCompletionType ):
430
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Dict ]:
438
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Set ]:
431
439
if "locals_" not in kwargs :
432
440
return None
433
441
locals_ = kwargs ["locals_" ]
434
442
435
443
r = self .locate (cursor_offset , line )
436
444
if r is None :
437
445
return None
438
- _ , _ , dexpr = lineparts .current_dict (cursor_offset , line )
446
+ curDictParts = lineparts .current_dict (cursor_offset , line )
447
+ assert curDictParts , "current_dict when .locate() truthy"
448
+ _ , _ , dexpr = curDictParts
439
449
try :
440
450
obj = safe_eval (dexpr , locals_ )
441
451
except EvaluationError :
@@ -456,7 +466,7 @@ def format(self, match):
456
466
457
467
458
468
class MagicMethodCompletion (BaseCompletionType ):
459
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Dict ]:
469
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Set ]:
460
470
if "current_block" not in kwargs :
461
471
return None
462
472
current_block = kwargs ["current_block" ]
@@ -508,7 +518,7 @@ def locate(self, current_offset, line) -> Union[LinePart, None]:
508
518
509
519
510
520
class ParameterNameCompletion (BaseCompletionType ):
511
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Dict ]:
521
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Set ]:
512
522
if "argspec" not in kwargs :
513
523
return None
514
524
argspec = kwargs ["argspec" ]
@@ -538,7 +548,7 @@ class ExpressionAttributeCompletion(AttrCompletion):
538
548
def locate (self , current_offset , line ) -> Union [LinePart , None ]:
539
549
return lineparts .current_expression_attribute (current_offset , line )
540
550
541
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [Set , Dict , None ]:
551
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [Set , None ]:
542
552
if "locals_" not in kwargs :
543
553
return None
544
554
locals_ = kwargs ["locals_" ]
@@ -547,6 +557,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, Dict, None]:
547
557
locals_ = __main__ .__dict__
548
558
549
559
attr = self .locate (cursor_offset , line )
560
+ assert attr , "locate was already truthy for the same call"
550
561
551
562
try :
552
563
obj = evaluate_current_expression (cursor_offset , line , locals_ )
@@ -570,7 +581,9 @@ def matches(self, cursor_offset, line, **kwargs) -> None:
570
581
else :
571
582
572
583
class JediCompletion (BaseCompletionType ):
573
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Dict ]:
584
+ _orig_start : Union [int , None ]
585
+
586
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [None , Set ]:
574
587
if "history" not in kwargs :
575
588
return None
576
589
history = kwargs ["history" ]
@@ -596,6 +609,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
596
609
else :
597
610
self ._orig_start = None
598
611
return None
612
+ assert isinstance (self ._orig_start , int )
599
613
600
614
first_letter = line [self ._orig_start : self ._orig_start + 1 ]
601
615
@@ -610,13 +624,14 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
610
624
# case-sensitive matches only
611
625
return {m for m in matches if m .startswith (first_letter )}
612
626
613
- def locate (self , cursor_offset , line ) -> LinePart :
627
+ def locate (self , cursor_offset : int , line : str ) -> LinePart :
628
+ assert isinstance (self ._orig_start , int )
614
629
start = self ._orig_start
615
630
end = cursor_offset
616
631
return LinePart (start , end , line [start :end ])
617
632
618
633
class MultilineJediCompletion (JediCompletion ):
619
- def matches (self , cursor_offset , line , ** kwargs ) -> Union [Dict , None ]:
634
+ def matches (self , cursor_offset , line , ** kwargs ) -> Union [Set , None ]:
620
635
if "current_block" not in kwargs or "history" not in kwargs :
621
636
return None
622
637
current_block = kwargs ["current_block" ]
0 commit comments