Skip to content

Commit

Permalink
Merge pull request #7546 from stuartarchibald/fix/7545
Browse files Browse the repository at this point in the history
Fix handling of missing const key in LiteralStrKeyDict
  • Loading branch information
sklam committed Nov 18, 2021
2 parents 2251a16 + c530a25 commit 3884e95
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion numba/core/typing/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,10 @@ def generic(self, args, kws):
if not isinstance(tup, types.LiteralStrKeyDict):
return
if isinstance(idx, str):
lookup = tup.fields.index(idx)
if idx in tup.fields:
lookup = tup.fields.index(idx)
else:
raise errors.NumbaKeyError(f"Key '{idx}' is not in dict.")
ret = tup.types[lookup]
if ret is not None:
sig = signature(ret, *args)
Expand Down
12 changes: 12 additions & 0 deletions numba/tests/test_dictobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,18 @@ def foo():

foo()

def test_const_key_not_in_dict(self):

@njit
def foo():
a = {'not_a': 2j, 'c': 'd', 'e': np.zeros(4)}
return a['a']

with self.assertRaises(TypingError) as raises:
foo()

self.assertIn("Key 'a' is not in dict.", str(raises.exception))

def test_uncommon_identifiers(self):
# Tests uncommon identifiers like numerical values and operators in
# the key fields. See #6518 and #7416.
Expand Down

0 comments on commit 3884e95

Please sign in to comment.