##// 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 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 970 def test_tryimport():
943 971 """
944 972 Test that try-import don't crash on trailing dot, and import modules before
@@ -6,6 +6,7 b''
6 6 # Distributed under the terms of the Modified BSD License.
7 7
8 8 import inspect
9 import types
9 10
10 11
11 12 def safe_hasattr(obj, attr):
@@ -53,16 +54,13 b' def dir2(obj):'
53 54 def get_real_method(obj, name):
54 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 58 - Check if obj is a proxy that claims to have all attributes
58 59 - Catch attribute access failing with any exception
59 60 - Check that the attribute is a callable object
60 61
61 62 Returns the method or None.
62 63 """
63 if inspect.isclass(obj):
64 return None
65
66 64 try:
67 65 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
68 66 except Exception:
@@ -77,6 +75,9 b' def get_real_method(obj, name):'
77 75 except Exception:
78 76 return None
79 77
78 if inspect.isclass(obj) and not isinstance(m, types.MethodType):
79 return None
80
80 81 if callable(m):
81 82 return m
82 83
General Comments 0
You need to be logged in to leave comments. Login now