Skip to content

Commit 285be46

Browse files
committed
disallow __classcell__ and __classdictcell__
1 parent 8f813c1 commit 285be46

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

Lib/test/test_syntax.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,18 +2610,14 @@ def test_continuation_bad_indentation(self):
26102610
@support.cpython_only
26112611
def test_disallowed_type_param_names(self):
26122612
# See gh-128632
2613-
self._check_error("class A[__class__]: pass",
2614-
"reserved name '__class__' can not be used for type parameter")
2615-
self._check_error("class A[__classdict__]: pass",
2616-
"reserved name '__classdict__' can not be used for type parameter")
2617-
self._check_error("def f[__class__](): pass",
2618-
"reserved name '__class__' can not be used for type parameter")
2619-
self._check_error("def f[__classdict__](): pass",
2620-
"reserved name '__classdict__' can not be used for type parameter")
2621-
self._check_error("type T[__class__] = tuple[__class__]",
2622-
"reserved name '__class__' can not be used for type parameter")
2623-
self._check_error("type T[__classdict__] = tuple[__classdict__]",
2624-
"reserved name '__classdict__' can not be used for type parameter")
2613+
2614+
for name in ('__class__', '__classdict__', '__classcell__', '__classdictcell__'):
2615+
self._check_error(f"class A[{name}]: pass",
2616+
f"reserved name '{name}' cannot be used for type parameter")
2617+
self._check_error(f"def f[{name}](): pass",
2618+
f"reserved name '{name}' cannot be used for type parameter")
2619+
self._check_error(f"type T[{name}] = tuple[{name}]",
2620+
f"reserved name '{name}' cannot be used for type parameter")
26252621

26262622
@support.cpython_only
26272623
def test_nested_named_except_blocks(self):

Python/symtable.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,9 @@ symtable_visit_type_param_bound_or_default(
25392539
type_param_ty tp, const char *ste_scope_info)
25402540
{
25412541
if (_PyUnicode_EqualToASCIIString(name, "__class__") ||
2542-
_PyUnicode_EqualToASCIIString(name, "__classdict__")) {
2542+
_PyUnicode_EqualToASCIIString(name, "__classdict__") ||
2543+
_PyUnicode_EqualToASCIIString(name, "__classcell__") ||
2544+
_PyUnicode_EqualToASCIIString(name, "__classdictcell__")) {
25432545

25442546
PyObject *error_msg = PyUnicode_FromFormat("reserved name '%U' cannot be "
25452547
"used for type parameter", name);

0 commit comments

Comments
 (0)