##// END OF EJS Templates
Support Bytes dict completions, and test it....
Matthias Bussonnier -
Show More
@@ -622,8 +622,27 b' def get__all__entries(obj):'
622 622 return [cast_unicode_py2(w) for w in words if isinstance(w, str)]
623 623
624 624
625 def match_dict_keys(keys, prefix, delims):
626 """Used by dict_key_matches, matching the prefix to a list of keys"""
625 def match_dict_keys(keys: List[str], prefix: str, delims: str):
626 """Used by dict_key_matches, matching the prefix to a list of keys
627
628 Parameters
629 ==========
630 keys:
631 list of keys in dictionary currently being completed.
632 prefix:
633 Part of the text already typed by the user. e.g. `mydict[b'fo`
634 delims:
635 String of delimiters to consider when finding the current key.
636
637 Returns
638 =======
639
640 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
641 ``quote`` being the quote that need to be used to close current string.
642 ``token_start`` the position where the replacement should start occurring,
643 ``matches`` a list of replacement/completion
644
645 """
627 646 if not prefix:
628 647 return None, 0, [repr(k) for k in keys
629 648 if isinstance(k, (str, bytes))]
@@ -639,7 +658,6 b' def match_dict_keys(keys, prefix, delims):'
639 658 token_start = token_match.start()
640 659 token_prefix = token_match.group()
641 660
642 # TODO: support bytes in Py3k
643 661 matched = []
644 662 for key in keys:
645 663 try:
@@ -652,7 +670,7 b' def match_dict_keys(keys, prefix, delims):'
652 670 # reformat remainder of key to begin with prefix
653 671 rem = key[len(prefix_str):]
654 672 # force repr wrapped in '
655 rem_repr = repr(rem + '"')
673 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
656 674 if rem_repr.startswith('u') and prefix[0] not in 'uU':
657 675 # Found key is unicode, but prefix is Py2 string.
658 676 # Therefore attempt to interpret key as string.
@@ -20,7 +20,7 b' from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory'
20 20 from IPython.utils.generics import complete_object
21 21 from IPython.testing import decorators as dec
22 22
23 from IPython.core.completer import Completion, provisionalcompleter
23 from IPython.core.completer import Completion, provisionalcompleter, match_dict_keys
24 24 from nose.tools import assert_in, assert_not_in
25 25
26 26 #-----------------------------------------------------------------------------
@@ -526,7 +526,28 b' def test_magic_completion_order():'
526 526
527 527 # Order of user variable and line and cell magics with same name:
528 528 text, matches = c.complete('timeit')
529 nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"])
529 nt.assert_equal(matches, ["timeit", "%timeit", "%%timeit"])
530
531 def test_match_dict_keys():
532 """
533 Test that match_dict_keys works on a couple of use case does return what
534 expected, and does not crash
535 """
536 delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
537
538
539 keys = ['foo', b'far']
540 assert match_dict_keys(keys, "b'", delims=delims) == ("'", 2 ,['far'])
541 assert match_dict_keys(keys, "b'f", delims=delims) == ("'", 2 ,['far'])
542 assert match_dict_keys(keys, 'b"', delims=delims) == ('"', 2 ,['far'])
543 assert match_dict_keys(keys, 'b"f', delims=delims) == ('"', 2 ,['far'])
544
545 assert match_dict_keys(keys, "'", delims=delims) == ("'", 1 ,['foo'])
546 assert match_dict_keys(keys, "'f", delims=delims) == ("'", 1 ,['foo'])
547 assert match_dict_keys(keys, '"', delims=delims) == ('"', 1 ,['foo'])
548 assert match_dict_keys(keys, '"f', delims=delims) == ('"', 1 ,['foo'])
549
550 match_dict_keys
530 551
531 552
532 553 def test_dict_key_completion_string():
General Comments 0
You need to be logged in to leave comments. Login now