##// END OF EJS Templates
Allow key completion for classes (#10903)
David Straub -
Show More
@@ -939,6 +939,34 b' def test_object_key_completion():'
939 nt.assert_in('qwick', matches)
939 nt.assert_in('qwick', matches)
940
940
941
941
942 class NamedInstanceMetaclass(type):
943 def __getitem__(cls, item):
944 return cls.get_instance(item)
945
946 class NamedInstanceClass(object, metaclass=NamedInstanceMetaclass):
947 def __init__(self, name):
948 if not hasattr(self.__class__, 'instances'):
949 self.__class__.instances = {}
950 self.__class__.instances[name] = self
951
952 @classmethod
953 def _ipython_key_completions_(cls):
954 return cls.instances.keys()
955
956 @classmethod
957 def get_instance(cls, name):
958 return cls.instances[name]
959
960 def test_class_key_completion():
961 ip = get_ipython()
962 NamedInstanceClass('qwerty')
963 NamedInstanceClass('qwick')
964 ip.user_ns['named_instance_class'] = NamedInstanceClass
965
966 _, matches = ip.Completer.complete(line_buffer="named_instance_class['qw")
967 nt.assert_in('qwerty', matches)
968 nt.assert_in('qwick', matches)
969
942 def test_tryimport():
970 def test_tryimport():
943 """
971 """
944 Test that try-import don't crash on trailing dot, and import modules before
972 Test that try-import don't crash on trailing dot, and import modules before
@@ -6,6 +6,7 b''
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8 import inspect
8 import inspect
9 import types
9
10
10
11
11 def safe_hasattr(obj, attr):
12 def safe_hasattr(obj, attr):
@@ -53,16 +54,13 b' def dir2(obj):'
53 def get_real_method(obj, name):
54 def get_real_method(obj, name):
54 """Like getattr, but with a few extra sanity checks:
55 """Like getattr, but with a few extra sanity checks:
55
56
56 - If obj is a class, ignore its methods
57 - If obj is a class, ignore everything except class methods
57 - Check if obj is a proxy that claims to have all attributes
58 - Check if obj is a proxy that claims to have all attributes
58 - Catch attribute access failing with any exception
59 - Catch attribute access failing with any exception
59 - Check that the attribute is a callable object
60 - Check that the attribute is a callable object
60
61
61 Returns the method or None.
62 Returns the method or None.
62 """
63 """
63 if inspect.isclass(obj):
64 return None
65
66 try:
64 try:
67 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
65 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
68 except Exception:
66 except Exception:
@@ -77,6 +75,9 b' def get_real_method(obj, name):'
77 except Exception:
75 except Exception:
78 return None
76 return None
79
77
78 if inspect.isclass(obj) and not isinstance(m, types.MethodType):
79 return None
80
80 if callable(m):
81 if callable(m):
81 return m
82 return m
82
83
General Comments 0
You need to be logged in to leave comments. Login now