https://github.com/python/cpython/commit/5ffefdb108094a44e84345561ab1f1b57ffda23f
commit: 5ffefdb108094a44e84345561ab1f1b57ffda23f
branch: main
author: Victorien <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-08-07T15:16:25-07:00
summary:
gh-155278: Avoid needlessly accessing `ForwardRef.__forward_code__` in
`evaluate_forward_ref()` (#155279)
files:
M Lib/typing.py
diff --git a/Lib/typing.py b/Lib/typing.py
index b56ba954ad38be..65e1d1ea6be584 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -25,6 +25,7 @@
import collections.abc
import copyreg
import functools
+import keyword
import operator
import sys
import types
@@ -998,8 +999,12 @@ def _make_forward_ref(code, *, parent_fwdref=None,
**kwargs):
if parent_fwdref.__owner__ is not None:
kwargs['owner'] = parent_fwdref.__owner__
forward_ref = annotationlib.ForwardRef(code, **kwargs)
- # For compatibility, eagerly compile the forwardref's code.
- forward_ref.__forward_code__
+ # For compatibility, eagerly compile the forwardref's code so that any
+ # SyntaxError is raised immediately rather than when the forward
+ # reference is evaluated. Similar to 'ForwardRef.evaluate()', we only
compile
+ # it if necessary:
+ if not (code.isidentifier() and not keyword.iskeyword(code)):
+ forward_ref.__forward_code__
return forward_ref
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]