From b489e295d01c654342f6c5a2c092b9f34c7a8b1e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 23 Jan 2026 15:31:40 +0100 Subject: [PATCH] gh-144169: Fix warning formatting in ast_type_init() Replace "%U" with "%R" since the argument can be an arbitrary object. --- Lib/test/test_ast/test_ast.py | 4 ++++ Parser/asdl_c.py | 2 +- Python/Python-ast.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py index 3917407fb37d9e..dfdec99a21771f 100644 --- a/Lib/test/test_ast/test_ast.py +++ b/Lib/test/test_ast/test_ast.py @@ -489,6 +489,10 @@ def test_classattrs(self): # Arbitrary keyword arguments are supported (but deprecated) with self.assertWarns(DeprecationWarning): self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar') + # gh-144169: non-string keys must not crash + with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): + ast.Constant(1, **{object(): 'bar'}) with self.assertRaisesRegex(TypeError, "Constant got multiple values for argument 'value'"): ast.Constant(1, value=2) diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 3e252cbc4883d1..6c6486d3fa7a78 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -965,7 +965,7 @@ def visitModule(self, mod): else if (contains == 0) { if (PyErr_WarnFormat( PyExc_DeprecationWarning, 1, - "%.400s.__init__ got an unexpected keyword argument '%U'. " + "%.400s.__init__ got an unexpected keyword argument %R. " "Support for arbitrary keyword arguments is deprecated " "and will be removed in Python 3.15.", Py_TYPE(self)->tp_name, key diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 79608dee9bfac2..6c77d2f5734319 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -5249,7 +5249,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) else if (contains == 0) { if (PyErr_WarnFormat( PyExc_DeprecationWarning, 1, - "%.400s.__init__ got an unexpected keyword argument '%U'. " + "%.400s.__init__ got an unexpected keyword argument %R. " "Support for arbitrary keyword arguments is deprecated " "and will be removed in Python 3.15.", Py_TYPE(self)->tp_name, key