unsigned ste_method : 1; /* true if block is a function block defined in class scope */
unsigned ste_has_conditional_annotations : 1; /* true if block has conditionally executed annotations */
unsigned ste_in_conditional_block : 1; /* set while we are inside a conditionally executed block */
+ unsigned ste_in_unevaluated_annotation : 1; /* set while we are processing an annotation that will not be evaluated */
int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
_Py_SourceLocation ste_loc; /* source location of block */
struct _symtable_entry *ste_annotation_block; /* symbol table entry for this entry's annotations */
import textwrap
import types
import unittest
-from test.support import run_code, check_syntax_error
+from test.support import run_code, check_syntax_error, cpython_only
class TypeAnnotationTests(unittest.TestCase):
del D.__annotations__
self.assertEqual(D.__annotations__, {})
+ @cpython_only
+ def test_no_cell(self):
+ # gh-130924: Test that uses of annotations in local scopes do not
+ # create cell variables.
+ def f(x):
+ a: x
+ return x
+
+ self.assertEqual(f.__code__.co_cellvars, ())
+
def build_module(code: str, name: str = "top") -> types.ModuleType:
ns = run_code(code)
check_syntax_error(self, prelude + "(x): (yield)", "yield expression cannot be used within an annotation")
check_syntax_error(self, prelude + "(x): (yield from x)", "yield expression cannot be used within an annotation")
check_syntax_error(self, prelude + "(x): (y := 3)", "named expression cannot be used within an annotation")
+ check_syntax_error(self, prelude + "(x): (__debug__ := 3)", "named expression cannot be used within an annotation")
check_syntax_error(self, prelude + "(x): (await 42)", "await expression cannot be used within an annotation")
def test_ignore_non_simple_annotations(self):
ste->ste_needs_classdict = 0;
ste->ste_has_conditional_annotations = 0;
ste->ste_in_conditional_block = 0;
+ ste->ste_in_unevaluated_annotation = 0;
ste->ste_annotation_block = NULL;
ste->ste_has_docstring = 0;
VISIT(st, expr, e->v.Slice.step);
break;
case Name_kind:
- if (!symtable_add_def_ctx(st, e->v.Name.id,
- e->v.Name.ctx == Load ? USE : DEF_LOCAL,
- LOCATION(e), e->v.Name.ctx)) {
- return 0;
- }
- /* Special-case super: it counts as a use of __class__ */
- if (e->v.Name.ctx == Load &&
- _PyST_IsFunctionLike(st->st_cur) &&
- _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
- if (!symtable_add_def(st, &_Py_ID(__class__), USE, LOCATION(e)))
+ if (!st->st_cur->ste_in_unevaluated_annotation) {
+ if (!symtable_add_def_ctx(st, e->v.Name.id,
+ e->v.Name.ctx == Load ? USE : DEF_LOCAL,
+ LOCATION(e), e->v.Name.ctx)) {
return 0;
+ }
+ /* Special-case super: it counts as a use of __class__ */
+ if (e->v.Name.ctx == Load &&
+ _PyST_IsFunctionLike(st->st_cur) &&
+ _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
+ if (!symtable_add_def(st, &_Py_ID(__class__), USE, LOCATION(e)))
+ return 0;
+ }
}
break;
/* child nodes of List and Tuple will have expr_context set */
static int
symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
{
+ // Annotations in local scopes are not executed and should not affect the symtable
+ bool is_unevaluated = st->st_cur->ste_type == FunctionBlock;
+
if ((st->st_cur->ste_type == ClassBlock || st->st_cur->ste_type == ModuleBlock)
&& st->st_cur->ste_in_conditional_block
&& !st->st_cur->ste_has_conditional_annotations)
return 0;
}
}
- VISIT(st, expr, annotation);
+ if (is_unevaluated) {
+ st->st_cur->ste_in_unevaluated_annotation = 1;
+ }
+ int rc = symtable_visit_expr(st, annotation);
+ if (is_unevaluated) {
+ st->st_cur->ste_in_unevaluated_annotation = 0;
+ }
if (!symtable_exit_block(st)) {
return 0;
}
- return 1;
+ return rc;
}
static int