Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,11 @@ class C[T]:
class Inner[U](make_base(T for _ in (1,)), make_base(T)):
pass
"""
with self.assertRaisesRegex(SyntaxError,
"Cannot use comprehension in annotation scope within class scope"):
run_code(code)
ns = run_code(code)
inner = ns["C"].Inner
base1, base2, _ = inner.__bases__
self.assertEqual(list(base1.__arg__), [ns["C"].__type_params__[0]])
self.assertEqual(base2.__arg__, "class")

def test_listcomp_in_nested_class(self):
code = """
Expand All @@ -464,9 +466,11 @@ class C[T]:
class Inner[U](make_base([T for _ in (1,)]), make_base(T)):
pass
"""
with self.assertRaisesRegex(SyntaxError,
"Cannot use comprehension in annotation scope within class scope"):
run_code(code)
ns = run_code(code)
inner = ns["C"].Inner
base1, base2, _ = inner.__bases__
self.assertEqual(base1.__arg__, [ns["C"].__type_params__[0]])
self.assertEqual(base2.__arg__, "class")

def test_gen_exp_in_generic_method(self):
code = """
Expand All @@ -475,27 +479,33 @@ class C[T]:
def meth[U](x: (T for _ in (1,)), y: T):
pass
"""
with self.assertRaisesRegex(SyntaxError,
"Cannot use comprehension in annotation scope within class scope"):
run_code(code)
ns = run_code(code)
meth = ns["C"].meth
self.assertEqual(list(meth.__annotations__["x"]), [ns["C"].__type_params__[0]])
self.assertEqual(meth.__annotations__["y"], "class")

def test_nested_scope_in_generic_alias(self):
code = """
class C[T]:
T = "global"
class C:
T = "class"
{}
"""
error_cases = [
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
"type Alias3[T] = (T for _ in (1,))",
"type Alias4 = (T for _ in (1,))",
"type Alias5[T] = [T for _ in (1,)]",
"type Alias6 = [T for _ in (1,)]",
"type Alias[T] = (T for _ in (1,))",
"type Alias = (T for _ in (1,))",
"type Alias[T] = [T for _ in (1,)]",
"type Alias = [T for _ in (1,)]",
]
for case in error_cases:
with self.subTest(case=case):
with self.assertRaisesRegex(SyntaxError,
r"Cannot use [a-z]+ in annotation scope within class scope"):
run_code(code.format(case))
ns = run_code(code.format(case))
alias = ns["C"].Alias
value = list(alias.__value__)[0]
if alias.__type_params__:
self.assertIs(value, alias.__type_params__[0])
else:
self.assertEqual(value, "global")

def test_lambda_in_alias_in_class(self):
code = """
Expand Down
5 changes: 4 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ static PyCodeObject *optimize_and_assemble(struct compiler *, int addNone);

#define CAPSULE_NAME "compile.c compiler unit"

static inline bool in_class_like_block(struct compiler *c) {
return c->u->u_ste->ste_type == ClassBlock || c->u->u_ste->ste_can_see_class_scope;
}

static int
compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
Expand Down Expand Up @@ -5460,7 +5463,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
PySTEntryObject *entry,
inlined_comprehension_state *state)
{
int in_class_block = (c->u->u_ste->ste_type == ClassBlock) && !c->u->u_in_inlined_comp;
int in_class_block = in_class_like_block(c) && !c->u->u_in_inlined_comp;
c->u->u_in_inlined_comp++;
// iterate over names bound in the comprehension and ensure we isolate
// them from the outer scope as needed
Expand Down
15 changes: 2 additions & 13 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,8 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
// free vars in comprehension that are locals in outer scope can
// now simply be locals, unless they are free in comp children,
// or if the outer scope is a class block
if (!is_free_in_any_child(comp, k) && ste->ste_type != ClassBlock) {
if (!is_free_in_any_child(comp, k) && ste->ste_type != ClassBlock
&& !ste->ste_can_see_class_scope) {
if (PySet_Discard(comp_free, k) < 0) {
return 0;
}
Expand Down Expand Up @@ -2589,18 +2590,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));
Expand Down