Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit425f60b

Browse files
gh-130924: Do not create cells for usages of names in local annotations (#131843)
1 parentc6b1a07 commit425f60b

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

‎Include/internal/pycore_symtable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ typedef struct _symtable_entry {
126126
unsignedste_method :1;/* true if block is a function block defined in class scope */
127127
unsignedste_has_conditional_annotations :1;/* true if block has conditionally executed annotations */
128128
unsignedste_in_conditional_block :1;/* set while we are inside a conditionally executed block */
129+
unsignedste_in_unevaluated_annotation :1;/* set while we are processing an annotation that will not be evaluated */
129130
intste_comp_iter_expr;/* non-zero if visiting a comprehension range expression */
130131
_Py_SourceLocationste_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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
importtextwrap
44
importtypes
55
importunittest
6-
fromtest.supportimportrun_code,check_syntax_error
6+
fromtest.supportimportrun_code,check_syntax_error,cpython_only
77

88

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

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

113123
defbuild_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
deftest_ignore_non_simple_annotations(self):
Lines changed: 3 additions & 0 deletions
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

Lines changed: 24 additions & 12 deletions
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
caseName_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-
return0;
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
return0;
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+
return0;
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
staticint
27342737
symtable_visit_annotation(structsymtable*st,expr_tyannotation,void*key)
27352738
{
2739+
// Annotations in local scopes are not executed and should not affect the symtable
2740+
boolis_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
return0;
27652771
}
27662772
}
2767-
VISIT(st,expr,annotation);
2773+
if (is_unevaluated) {
2774+
st->st_cur->ste_in_unevaluated_annotation=1;
2775+
}
2776+
intrc=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
return0;
27702782
}
2771-
return1;
2783+
returnrc;
27722784
}
27732785

27742786
staticint

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp