Skip to content

Commit 425f60b

Browse files
gh-130924: Do not create cells for usages of names in local annotations (#131843)
1 parent c6b1a07 commit 425f60b

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

Include/internal/pycore_symtable.h

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ typedef struct _symtable_entry {
126126
unsigned ste_method : 1; /* true if block is a function block defined in class scope */
127127
unsigned ste_has_conditional_annotations : 1; /* true if block has conditionally executed annotations */
128128
unsigned ste_in_conditional_block : 1; /* set while we are inside a conditionally executed block */
129+
unsigned ste_in_unevaluated_annotation : 1; /* set while we are processing an annotation that will not be evaluated */
129130
int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
130131
_Py_SourceLocation ste_loc; /* source location of block */
131132
struct _symtable_entry *ste_annotation_block; /* symbol table entry for this entry's annotations */

Lib/test/test_type_annotations.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import textwrap
44
import types
55
import unittest
6-
from test.support import run_code, check_syntax_error
6+
from test.support import run_code, check_syntax_error, cpython_only
77

88

99
class TypeAnnotationTests(unittest.TestCase):
@@ -109,6 +109,16 @@ class D(metaclass=C):
109109
del D.__annotations__
110110
self.assertEqual(D.__annotations__, {})
111111

112+
@cpython_only
113+
def test_no_cell(self):
114+
# gh-130924: Test that uses of annotations in local scopes do not
115+
# create cell variables.
116+
def f(x):
117+
a: x
118+
return x
119+
120+
self.assertEqual(f.__code__.co_cellvars, ())
121+
112122

113123
def build_module(code: str, name: str = "top") -> types.ModuleType:
114124
ns = run_code(code)
@@ -352,6 +362,7 @@ def test_no_exotic_expressions_in_unevaluated_annotations(self):
352362
check_syntax_error(self, prelude + "(x): (yield)", "yield expression cannot be used within an annotation")
353363
check_syntax_error(self, prelude + "(x): (yield from x)", "yield expression cannot be used within an annotation")
354364
check_syntax_error(self, prelude + "(x): (y := 3)", "named expression cannot be used within an annotation")
365+
check_syntax_error(self, prelude + "(x): (__debug__ := 3)", "named expression cannot be used within an annotation")
355366
check_syntax_error(self, prelude + "(x): (await 42)", "await expression cannot be used within an annotation")
356367

357368
def test_ignore_non_simple_annotations(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Usage of a name in a function-scope annotation no longer triggers creation
2+
of a cell for that variable. This fixes a regression in earlier alphas of
3+
Python 3.14.

Python/symtable.c

+24-12
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
141141
ste->ste_needs_classdict = 0;
142142
ste->ste_has_conditional_annotations = 0;
143143
ste->ste_in_conditional_block = 0;
144+
ste->ste_in_unevaluated_annotation = 0;
144145
ste->ste_annotation_block = NULL;
145146

146147
ste->ste_has_docstring = 0;
@@ -2538,17 +2539,19 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
25382539
VISIT(st, expr, e->v.Slice.step);
25392540
break;
25402541
case Name_kind:
2541-
if (!symtable_add_def_ctx(st, e->v.Name.id,
2542-
e->v.Name.ctx == Load ? USE : DEF_LOCAL,
2543-
LOCATION(e), e->v.Name.ctx)) {
2544-
return 0;
2545-
}
2546-
/* Special-case super: it counts as a use of __class__ */
2547-
if (e->v.Name.ctx == Load &&
2548-
_PyST_IsFunctionLike(st->st_cur) &&
2549-
_PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
2550-
if (!symtable_add_def(st, &_Py_ID(__class__), USE, LOCATION(e)))
2542+
if (!st->st_cur->ste_in_unevaluated_annotation) {
2543+
if (!symtable_add_def_ctx(st, e->v.Name.id,
2544+
e->v.Name.ctx == Load ? USE : DEF_LOCAL,
2545+
LOCATION(e), e->v.Name.ctx)) {
25512546
return 0;
2547+
}
2548+
/* Special-case super: it counts as a use of __class__ */
2549+
if (e->v.Name.ctx == Load &&
2550+
_PyST_IsFunctionLike(st->st_cur) &&
2551+
_PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
2552+
if (!symtable_add_def(st, &_Py_ID(__class__), USE, LOCATION(e)))
2553+
return 0;
2554+
}
25522555
}
25532556
break;
25542557
/* child nodes of List and Tuple will have expr_context set */
@@ -2733,6 +2736,9 @@ symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
27332736
static int
27342737
symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
27352738
{
2739+
// Annotations in local scopes are not executed and should not affect the symtable
2740+
bool is_unevaluated = st->st_cur->ste_type == FunctionBlock;
2741+
27362742
if ((st->st_cur->ste_type == ClassBlock || st->st_cur->ste_type == ModuleBlock)
27372743
&& st->st_cur->ste_in_conditional_block
27382744
&& !st->st_cur->ste_has_conditional_annotations)
@@ -2764,11 +2770,17 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
27642770
return 0;
27652771
}
27662772
}
2767-
VISIT(st, expr, annotation);
2773+
if (is_unevaluated) {
2774+
st->st_cur->ste_in_unevaluated_annotation = 1;
2775+
}
2776+
int rc = symtable_visit_expr(st, annotation);
2777+
if (is_unevaluated) {
2778+
st->st_cur->ste_in_unevaluated_annotation = 0;
2779+
}
27682780
if (!symtable_exit_block(st)) {
27692781
return 0;
27702782
}
2771-
return 1;
2783+
return rc;
27722784
}
27732785

27742786
static int

0 commit comments

Comments
 (0)