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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Desired behavior
  • Loading branch information
JelleZijlstra committed Apr 22, 2024
commit ad56c5fbc2c598ead76bc9a1bbbd31b9287a193e
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
12 changes: 0 additions & 12 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2589,18 +2589,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