FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-109118: Allow lambdas in annotation scopes in classes by JelleZijlstra · Pull Request #118019 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.13.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ Other Language Changes
ones if configured to do so.
(Contributed by Pedro Sousa Lacerda in :gh:`66449`.)

* :ref:`annotation scope <annotation-scopes>` within class scopes can now
contain lambdas. (Contributed by Jelle Zijlstra in :gh:`109118`.)


New Modules
===========
Expand Down
50 changes: 48 additions & 2 deletions Lib/test/test_type_params.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,6 @@ class C[T]:
{}
"""
error_cases = [
"type Alias1[T] = lambda: T",
"type Alias2 = lambda: T",
"type Alias3[T] = (T for _ in (1,))",
"type Alias4 = (T for _ in (1,))",
"type Alias5[T] = [T for _ in (1,)]",
Expand All @@ -499,6 +497,54 @@ class C[T]:
r"Cannot use [a-z]+ in annotation scope within class scope"):
run_code(code.format(case))

def test_lambda_in_alias_in_class(self):
code = """
T = "global"
class C:
T = "class"
type Alias = lambda: T
"""
C = run_code(code)["C"]
self.assertEqual(C.Alias.__value__(), "global")

def test_lambda_in_alias_in_generic_class(self):
code = """
class C[T]:
T = "class"
type Alias = lambda: T
"""
C = run_code(code)["C"]
self.assertIs(C.Alias.__value__(), C.__type_params__[0])

def test_lambda_in_generic_alias_in_class(self):
# A lambda nested in the alias cannot see the class scope, but can see
# a surrounding annotation scope.
code = """
T = U = "global"
class C:
T = "class"
U = "class"
type Alias[T] = lambda: (T, U)
"""
C = run_code(code)["C"]
T, U = C.Alias.__value__()
self.assertIs(T, C.Alias.__type_params__[0])
self.assertEqual(U, "global")

def test_lambda_in_generic_alias_in_generic_class(self):
# A lambda nested in the alias cannot see the class scope, but can see
# a surrounding annotation scope.
code = """
class C[T, U]:
T = "class"
U = "class"
type Alias[T] = lambda: (T, U)
"""
C = run_code(code)["C"]
T, U = C.Alias.__value__()
self.assertIs(T, C.Alias.__type_params__[0])
self.assertIs(U, C.__type_params__[1])


def make_base(arg):
class Base:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:ref:`annotation scope <annotation-scopes>` within class scopes can now
contain lambdas.
11 changes: 0 additions & 11 deletions Python/symtable.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2140,17 +2140,6 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
VISIT(st, expr, e->v.UnaryOp.operand);
break;
case Lambda_kind: {
if (st->st_cur->ste_can_see_class_scope) {
// gh-109118
PyErr_Format(PyExc_SyntaxError,
"Cannot use lambda 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);
}
if (e->v.Lambda.args->defaults)
VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
if (e->v.Lambda.args->kw_defaults)
Expand Down

Back | FazBrowse Home | New Git URL