diff --git a/numba/core/withcontexts.py b/numba/core/withcontexts.py index fb3c783fa5b..5a21ab57afe 100644 --- a/numba/core/withcontexts.py +++ b/numba/core/withcontexts.py @@ -225,8 +225,13 @@ def strip_var_ver(x): # Verify that all outputs are annotated not_annotated = set(stripped_outs) - set(typeanns) if not_annotated: - msg = 'missing type annotation on outgoing variables: {}' - raise errors.TypingError(msg.format(not_annotated)) + msg = ( + 'Missing type annotation on outgoing variable(s): {0}\n\n' + 'Example code: with objmode({1}=\'<' + 'add_type_as_string_here>\')\n' + ) + stable_ann = sorted(not_annotated) + raise errors.TypingError(msg.format(stable_ann, stable_ann[0])) # Get output types outtup = types.Tuple([typeanns[v] for v in stripped_outs]) diff --git a/numba/tests/test_withlifting.py b/numba/tests/test_withlifting.py index 341a632cc31..f0a50b0de24 100644 --- a/numba/tests/test_withlifting.py +++ b/numba/tests/test_withlifting.py @@ -522,15 +522,21 @@ def test_case07_mystery_key_error(self): def foo(x): with objmode_context(): t = {'a': x} - return x, t + u = 3 + return x, t, u x = np.array([1, 2, 3]) cfoo = njit(foo) + with self.assertRaises(errors.TypingError) as raises: cfoo(x) - self.assertIn( - "missing type annotation on outgoing variables", - str(raises.exception), - ) + + exstr = str(raises.exception) + self.assertIn("Missing type annotation on outgoing variable(s): " + "['t', 'u']", + exstr) + self.assertIn("Example code: with objmode" + "(t='')", + exstr) def test_case08_raise_from_external(self): # this segfaults, expect its because the dict needs to raise as '2' is