From 73dfb6661dd02f62bc1b3149b11eee4e681d1c9f Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Thu, 13 Nov 2025 15:03:09 +1030 Subject: [PATCH 1/2] Simplify closure/freevar iteration in `_build_closure()` --- Lib/annotationlib.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py index 33907b1fc2a53a..636247163cd1dc 100644 --- a/Lib/annotationlib.py +++ b/Lib/annotationlib.py @@ -844,14 +844,9 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False): def _build_closure(annotate, owner, is_class, stringifier_dict, *, allow_evaluation): if not annotate.__closure__: return None, None - freevars = annotate.__code__.co_freevars new_closure = [] cell_dict = {} - for i, cell in enumerate(annotate.__closure__): - if i < len(freevars): - name = freevars[i] - else: - name = "__cell__" + for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__): cell_dict[name] = cell new_cell = None if allow_evaluation: From 93083ceead878bd19cb7c16707db0572e86ad288 Mon Sep 17 00:00:00 2001 From: dr-carlos <77367421+dr-carlos@users.noreply.github.com> Date: Fri, 14 Nov 2025 06:52:44 +1030 Subject: [PATCH 2/2] Raise an error if the annotate function's freevars and closure mismatch Co-authored-by: Jelle Zijlstra --- Lib/annotationlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py index 636247163cd1dc..a5788cdbfae3f5 100644 --- a/Lib/annotationlib.py +++ b/Lib/annotationlib.py @@ -846,7 +846,7 @@ def _build_closure(annotate, owner, is_class, stringifier_dict, *, allow_evaluat return None, None new_closure = [] cell_dict = {} - for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__): + for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__, strict=True): cell_dict[name] = cell new_cell = None if allow_evaluation: