summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-04-28 13:21:28 (GMT)
committerGitHub <noreply@github.com>2024-04-28 13:21:28 (GMT)
commit2326d6c868e300a814179d77fc308fec8365cb8c (patch)
treef8d72b1b5065bd8fcf31f55b41d3e6d42039f81a /Python
parent51aefc5bf907ddffaaf083ded0de773adcdf08c8 (diff)
downloadcpython-2326d6c868e300a814179d77fc308fec8365cb8c.zip
cpython-2326d6c868e300a814179d77fc308fec8365cb8c.tar.gz
cpython-2326d6c868e300a814179d77fc308fec8365cb8c.tar.bz2
gh-109118: Make comprehensions work within annotation scopes, but without inlining (#118160)
Co-authored-by: Carl Meyer <carl@oddbird.net>
Diffstat (limited to 'Python')
-rw-r--r--Python/symtable.c18
1 files changed, 4 insertions, 14 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index 483ef1c..eecd159 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -1154,10 +1154,12 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
}
}
- // we inline all non-generator-expression comprehensions
+ // we inline all non-generator-expression comprehensions,
+ // except those in annotation scopes that are nested in classes
int inline_comp =
entry->ste_comprehension &&
- !entry->ste_generator;
+ !entry->ste_generator &&
+ !ste->ste_can_see_class_scope;
if (!analyze_child_block(entry, newbound, newfree, newglobal,
type_params, new_class_entry, &child_free))
@@ -2589,18 +2591,6 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
identifier scope_name, asdl_comprehension_seq *generators,
expr_ty elt, expr_ty value)
{
- if (st->st_cur->ste_can_see_class_scope) {
- // gh-109118
- PyErr_Format(PyExc_SyntaxError,
- "Cannot use comprehension in annotation scope within class scope");
- PyErr_RangedSyntaxLocationObject(st->st_filename,
- e->lineno,
- e->col_offset + 1,
- e->end_lineno,
- e->end_col_offset + 1);
- VISIT_QUIT(st, 0);
- }
-
int is_generator = (e->kind == GeneratorExp_kind);
comprehension_ty outermost = ((comprehension_ty)
asdl_seq_GET(generators, 0));