diff options
| author | Jelle Zijlstra <jelle.zijlstra@gmail.com> | 2025-03-29 21:15:48 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-29 21:15:48 (GMT) |
| commit | 425f60b9eb253c57bc32b453a02f1cf09963f85a (patch) | |
| tree | 53689bbfb79f157dfd73262daa68e6e0f8adf20c /Python/symtable.c | |
| parent | c6b1a073438d93d4e62957accc73487df6711851 (diff) | |
| download | cpython-425f60b9eb253c57bc32b453a02f1cf09963f85a.zip cpython-425f60b9eb253c57bc32b453a02f1cf09963f85a.tar.gz cpython-425f60b9eb253c57bc32b453a02f1cf09963f85a.tar.bz2 | |
gh-130924: Do not create cells for usages of names in local annotations (#131843)
Diffstat (limited to 'Python/symtable.c')
| -rw-r--r-- | Python/symtable.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/Python/symtable.c b/Python/symtable.c index 622cd52..748bd44 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -141,6 +141,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block, 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; @@ -2538,17 +2539,19 @@ symtable_visit_expr(struct symtable *st, expr_ty e) 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 */ @@ -2733,6 +2736,9 @@ symtable_visit_params(struct symtable *st, asdl_arg_seq *args) 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) @@ -2764,11 +2770,17 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key) 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 |
