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
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Lib/annotationlib.py +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,9 @@ class Format(enum.IntEnum): |
27 | 27 |
|
28 | 28 |
|
29 | 29 | _sentinel = object() |
| 30 | +# Following `NAME_ERROR_MSG` in `ceval_macros.h`: |
| 31 | +_NAME_ERROR_MSG = "name '{name:.200}' is not defined" |
| 32 | + |
30 | 33 |
|
31 | 34 | # Slots shared by ForwardRef and _Stringifier. The __forward__ names must be |
32 | 35 | # preserved for compatibility with the old typing.ForwardRef class. The remaining |
@@ -184,7 +187,7 @@ def evaluate( |
184 | 187 | elif is_forwardref_format: |
185 | 188 | return self |
186 | 189 | else: |
187 | | - raise NameError(arg) |
| 190 | + raise NameError(_NAME_ERROR_MSG.format(name=arg), name=arg) |
188 | 191 | else: |
189 | 192 | code = self.__forward_code__ |
190 | 193 | try: |
|
Collapse file
Copy file name to clipboardExpand all lines: Lib/test/test_annotationlib.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1650,9 +1650,11 @@ def test_name_lookup_without_eval(self): |
1650 | 1650 | with support.swap_attr(builtins, "int", dict): |
1651 | 1651 | self.assertIs(ForwardRef("int").evaluate(), dict) |
1652 | 1652 |
|
1653 | | - with self.assertRaises(NameError): |
| 1653 | + with self.assertRaises(NameError, msg="name 'doesntexist' is not defined") as exc: |
1654 | 1654 | ForwardRef("doesntexist").evaluate() |
1655 | 1655 |
|
| 1656 | + self.assertEqual(exc.exception.name, "doesntexist") |
| 1657 | + |
1656 | 1658 | def test_fwdref_invalid_syntax(self): |
1657 | 1659 | fr = ForwardRef("if") |
1658 | 1660 | with self.assertRaises(SyntaxError): |
|
Collapse file
Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +Raise consistent :exc:`NameError` exceptions in :func:`annotationlib.ForwardRef.evaluate` |