@@ -476,15 +476,20 @@ def _get_item_lookup_completions(
476
476
Complete dictionary keys.
477
477
"""
478
478
479
- def meta_repr (value : object ) -> Callable [[], str ]:
479
+ def meta_repr (obj : object , key : object ) -> Callable [[], str ]:
480
480
"Abbreviate meta text, make sure it fits on one line."
481
481
482
482
# We return a function, so that it gets computed when it's needed.
483
483
# When there are many completions, that improves the performance
484
484
# quite a bit (for the multi-column completion menu, we only need
485
485
# to display one meta text).
486
486
def get_value_repr () -> str :
487
- text = self ._do_repr (value )
487
+ try :
488
+ value = obj [key ] # type: ignore
489
+
490
+ text = self ._do_repr (value )
491
+ except BaseException :
492
+ return "-"
488
493
489
494
# Take first line, if multiple lines.
490
495
if "\n " in text :
@@ -504,24 +509,24 @@ def get_value_repr() -> str:
504
509
# If this object is a dictionary, complete the keys.
505
510
if isinstance (result , (dict , collections_abc .Mapping )):
506
511
# Try to evaluate the key.
507
- key_obj = key
512
+ key_obj_str = str ( key )
508
513
for k in [key , key + '"' , key + "'" ]:
509
514
try :
510
- key_obj = ast .literal_eval (k )
515
+ key_obj_str = str ( ast .literal_eval (k ) )
511
516
except (SyntaxError , ValueError ):
512
517
continue
513
518
else :
514
519
break
515
520
516
- for k , v in result . items () :
517
- if str (k ).startswith (str ( key_obj ) ):
521
+ for k in result :
522
+ if str (k ).startswith (key_obj_str ):
518
523
try :
519
524
k_repr = self ._do_repr (k )
520
525
yield Completion (
521
526
k_repr + "]" ,
522
527
- len (key ),
523
528
display = f"[{ k_repr } ]" ,
524
- display_meta = meta_repr (v ),
529
+ display_meta = meta_repr (result , k ),
525
530
)
526
531
except ReprFailedError :
527
532
pass
@@ -537,7 +542,7 @@ def get_value_repr() -> str:
537
542
k_repr + "]" ,
538
543
- len (key ),
539
544
display = f"[{ k_repr } ]" ,
540
- display_meta = meta_repr (result [ k ] ),
545
+ display_meta = meta_repr (result , k ),
541
546
)
542
547
except KeyError :
543
548
# `result[k]` lookup failed. Trying to complete
0 commit comments