##// END OF EJS Templates
Make dict key completion more conservative...
Joel Nothman -
Show More
@@ -97,6 +97,21 b" if sys.platform == 'win32':"
97 else:
97 else:
98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
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 # Main functions and classes
116 # Main functions and classes
102 #-----------------------------------------------------------------------------
117 #-----------------------------------------------------------------------------
@@ -856,14 +871,15 b' class IPCompleter(Completer):'
856
871
857 def dict_key_matches(self, text):
872 def dict_key_matches(self, text):
858 def get_keys(obj):
873 def get_keys(obj):
859 if not callable(getattr(obj, '__getitem__', None)):
874 # Only allow completion for known in-memory dict-like types
860 return []
875 if isinstance(obj, KEYED_DICT_TYPES):
861 if hasattr(obj, 'keys'):
862 try:
876 try:
863 return list(obj.keys())
877 return list(obj.keys())
864 except Exception:
878 except Exception:
865 return []
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 try:
884 try:
869 regexps = self.__dict_key_regexps
885 regexps = self.__dict_key_regexps
@@ -584,21 +584,6 b' def test_dict_key_completion_unicode_py3():'
584 nt.assert_in(unicode_type(b"a\xd7\x90']", 'utf8'), matches)
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 @dec.skip_without('numpy')
587 @dec.skip_without('numpy')
603 def test_struct_array_key_completion():
588 def test_struct_array_key_completion():
604 """Test dict key completion applies to numpy struct arrays"""
589 """Test dict key completion applies to numpy struct arrays"""
General Comments 0
You need to be logged in to leave comments. Login now