X Tutup
Skip to content

Commit 9a79231

Browse files
Merge branch 'autocomplete-test'
Conflicts: bpython/autocomplete.py
2 parents 02c2412 + 796ae49 commit 9a79231

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

bpython/autocomplete.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def matches(self, cursor_offset, line, locals_, **kwargs):
219219
obj = safe_eval(dexpr, locals_)
220220
except EvaluationError:
221221
return set()
222-
if obj and isinstance(obj, type({})) and obj.keys():
223-
return set("{!r}]".format(k) for k in obj.keys()
222+
if isinstance(obj, dict) and obj.keys():
223+
return set("{0!r}]".format(k) for k in obj.keys()
224224
if repr(k).startswith(orig))
225225
else:
226226
return set()

bpython/test/test_autocomplete.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,33 @@ def test_formatting_takes_just_last_part(self):
145145
self.assertEqual(self.completer.format('/hello/there/'), 'there/')
146146
self.assertEqual(self.completer.format('/hello/there'), 'there')
147147

148+
class MockNumPy(object):
149+
"""
150+
This is a mock numpy object that raises an error when there is an atempt to convert it to a boolean.
151+
"""
152+
def __nonzero__(self):
153+
raise ValueError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")
154+
155+
156+
class TestDictKeyCompletion(unittest.TestCase):
157+
158+
def test_set_of_keys_returned_when_matches_found(self):
159+
com = autocomplete.DictKeyCompletion()
160+
local={'d':{"ab":1, "cd":2},}
161+
com.matches(2,"d[" , local)
162+
self.assertSetEqual(com.matches(2,"d[" , local),set(["'ab']","'cd']"]))
163+
164+
def test_empty_set_returned_when_eval_error(self):
165+
com = autocomplete.DictKeyCompletion()
166+
local={'e':{"ab":1, "cd":2},}
167+
self.assertSetEqual(com.matches(2,"d[" , local),set())
168+
169+
def test_empty_set_returned_when_not_dict_type(self):
170+
com = autocomplete.DictKeyCompletion()
171+
local={'l':["ab", "cd"],}
172+
self.assertSetEqual(com.matches(2,"l[" , local),set())
173+
174+
def test_obj_that_does_not_allow_conversion_to_bool(self):
175+
com = autocomplete.DictKeyCompletion()
176+
local={'mNumPy':MockNumPy(),}
177+
self.assertSetEqual(com.matches(7,"mNumPy[" , local), set())

0 commit comments

Comments
 (0)
X Tutup