Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be59d14

Browse files
committed
Issue #26146: enhance ast.Constant error message
Mention the name of the invalid type in error message of AST validation for constants. Suggestion made by Joseph Jevnik on a review.
1 parent 25219f5 commit be59d14

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/test/test_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,12 @@ def compile_constant(self, value):
951951
exec(code, ns)
952952
return ns['x']
953953

954+
def test_validation(self):
955+
with self.assertRaises(TypeError) as cm:
956+
self.compile_constant([1, 2, 3])
957+
self.assertEqual(str(cm.exception),
958+
"got an invalid type in Constant: list")
959+
954960
def test_singletons(self):
955961
for const in (None, False, True, Ellipsis, b'', frozenset()):
956962
with self.subTest(const=const):

Python/ast.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
288288
validate_keywords(exp->v.Call.keywords);
289289
case Constant_kind:
290290
if (!validate_constant(exp->v.Constant.value)) {
291-
PyErr_SetString(PyExc_TypeError, "invalid type in Constant");
291+
PyErr_Format(PyExc_TypeError,
292+
"got an invalid type in Constant: %s",
293+
Py_TYPE(exp->v.Constant.value)->tp_name);
292294
return 0;
293295
}
294296
return 1;

0 commit comments

Comments
 (0)