https://github.com/python/cpython/commit/d4daf4da7ec9c4ff93d713c511d6e96d8c9fe9fa
commit: d4daf4da7ec9c4ff93d713c511d6e96d8c9fe9fa
branch: 3.14
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: JelleZijlstra <jelle.zijls...@gmail.com>
date: 2025-06-18T13:26:58Z
summary:

[3.14] gh-135646: Raise consistent `NameError` exceptions in 
`ForwardRef.evaluate()` (GH-135663) (#135673)

gh-135646: Raise consistent `NameError` exceptions in `ForwardRef.evaluate()` 
(GH-135663)
(cherry picked from commit 343719d98e60d28d6102002f8ad3fd9dc5a58bd1)

Co-authored-by: Victorien <65306057+vii...@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2025-06-18-11-43-17.gh-issue-135646.r7ekEn.rst
M Lib/annotationlib.py
M Lib/test/test_annotationlib.py

diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py
index 731817a9973799..c83a1573ccd3d1 100644
--- a/Lib/annotationlib.py
+++ b/Lib/annotationlib.py
@@ -27,6 +27,9 @@ class Format(enum.IntEnum):
 
 
 _sentinel = object()
+# Following `NAME_ERROR_MSG` in `ceval_macros.h`:
+_NAME_ERROR_MSG = "name '{name:.200}' is not defined"
+
 
 # Slots shared by ForwardRef and _Stringifier. The __forward__ names must be
 # preserved for compatibility with the old typing.ForwardRef class. The 
remaining
@@ -184,7 +187,7 @@ def evaluate(
             elif is_forwardref_format:
                 return self
             else:
-                raise NameError(arg)
+                raise NameError(_NAME_ERROR_MSG.format(name=arg), name=arg)
         else:
             code = self.__forward_code__
             try:
diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py
index fe091e52a86dc4..ae0e73f08c5bd0 100644
--- a/Lib/test/test_annotationlib.py
+++ b/Lib/test/test_annotationlib.py
@@ -1650,9 +1650,11 @@ def test_name_lookup_without_eval(self):
         with support.swap_attr(builtins, "int", dict):
             self.assertIs(ForwardRef("int").evaluate(), dict)
 
-        with self.assertRaises(NameError):
+        with self.assertRaises(NameError, msg="name 'doesntexist' is not 
defined") as exc:
             ForwardRef("doesntexist").evaluate()
 
+        self.assertEqual(exc.exception.name, "doesntexist")
+
     def test_fwdref_invalid_syntax(self):
         fr = ForwardRef("if")
         with self.assertRaises(SyntaxError):
diff --git 
a/Misc/NEWS.d/next/Library/2025-06-18-11-43-17.gh-issue-135646.r7ekEn.rst 
b/Misc/NEWS.d/next/Library/2025-06-18-11-43-17.gh-issue-135646.r7ekEn.rst
new file mode 100644
index 00000000000000..5fbd751467dd58
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-06-18-11-43-17.gh-issue-135646.r7ekEn.rst
@@ -0,0 +1 @@
+Raise consistent :exc:`NameError` exceptions in 
:func:`annotationlib.ForwardRef.evaluate`

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to