GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-135646: Raise consistent `NameError` exceptions in `ForwardRef.eva… · python/cpython@d28e0a9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Lib
    • annotationlib.py
    • test
      • test_annotationlib.py
  • Misc/NEWS.d/next/Library
    • 2025-06-18-11-43-17.gh-issue-135646.r7ekEn.rst
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/annotationlib.py‎

Copy file name to clipboardExpand all lines: Lib/annotationlib.py
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Format(enum.IntEnum):
2727

2828

2929
_sentinel = object()
30+
# Following `NAME_ERROR_MSG` in `ceval_macros.h`:
31+
_NAME_ERROR_MSG = "name '{name:.200}' is not defined"
32+
3033

3134
# Slots shared by ForwardRef and _Stringifier. The __forward__ names must be
3235
# preserved for compatibility with the old typing.ForwardRef class. The remaining
@@ -184,7 +187,7 @@ def evaluate(
184187
elif is_forwardref_format:
185188
return self
186189
else:
187-
raise NameError(arg)
190+
raise NameError(_NAME_ERROR_MSG.format(name=arg), name=arg)
188191
else:
189192
code = self.__forward_code__
190193
try:
Collapse file

‎Lib/test/test_annotationlib.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_annotationlib.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,9 +1650,11 @@ def test_name_lookup_without_eval(self):
16501650
with support.swap_attr(builtins, "int", dict):
16511651
self.assertIs(ForwardRef("int").evaluate(), dict)
16521652

1653-
with self.assertRaises(NameError):
1653+
with self.assertRaises(NameError, msg="name 'doesntexist' is not defined") as exc:
16541654
ForwardRef("doesntexist").evaluate()
16551655

1656+
self.assertEqual(exc.exception.name, "doesntexist")
1657+
16561658
def test_fwdref_invalid_syntax(self):
16571659
fr = ForwardRef("if")
16581660
with self.assertRaises(SyntaxError):
Collapse file

‎Misc/NEWS.d/next/Library/2025-06-18-11-43-17.gh-issue-135646.r7ekEn.rst‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise consistent :exc:`NameError` exceptions in :func:`annotationlib.ForwardRef.evaluate`

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.