##// END OF EJS Templates
Make dict key completion more conservative...
Joel Nothman -
Show More
@@ -97,6 +97,21 b" if sys.platform == 'win32':"
97 97 else:
98 98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
99 99
100
101 # For dict key completion
102 try:
103 import numpy
104 except ImportError:
105 STRUCT_ARRAY_TYPES = ()
106 else:
107 STRUCT_ARRAY_TYPES = (numpy.ndarray,)
108 try:
109 import pandas
110 except ImportError:
111 KEYED_DICT_TYPES = (dict,)
112 else:
113 KEYED_DICT_TYPES = (dict, pandas.DataFrame)
114
100 115 #-----------------------------------------------------------------------------
101 116 # Main functions and classes
102 117 #-----------------------------------------------------------------------------
@@ -856,14 +871,15 b' class IPCompleter(Completer):'
856 871
857 872 def dict_key_matches(self, text):
858 873 def get_keys(obj):
859 if not callable(getattr(obj, '__getitem__', None)):
860 return []
861 if hasattr(obj, 'keys'):
874 # Only allow completion for known in-memory dict-like types
875 if isinstance(obj, KEYED_DICT_TYPES):
862 876 try:
863 877 return list(obj.keys())
864 878 except Exception:
865 879 return []
866 return getattr(getattr(obj, 'dtype', None), 'names', [])
880 elif isinstance(obj, STRUCT_ARRAY_TYPES):
881 return obj.dtype.names or []
882 return []
867 883
868 884 try:
869 885 regexps = self.__dict_key_regexps
@@ -584,21 +584,6 b' def test_dict_key_completion_unicode_py3():'
584 584 nt.assert_in(unicode_type(b"a\xd7\x90']", 'utf8'), matches)
585 585
586 586
587 def test_dict_like_key_completion():
588 """Test dict key completion applies where __getitem__ and keys exist"""
589 class D(object):
590 def __getitem__(self):
591 pass
592 def keys(self):
593 return iter(['hello', 'world'])
594 ip = get_ipython()
595 complete = ip.Completer.complete
596 ip.user_ns['d'] = D()
597 _, matches = complete(line_buffer="d['")
598 nt.assert_in("hello']", matches)
599 nt.assert_in("world']", matches)
600
601
602 587 @dec.skip_without('numpy')
603 588 def test_struct_array_key_completion():
604 589 """Test dict key completion applies to numpy struct arrays"""
General Comments 0
You need to be logged in to leave comments. Login now