From 3b04203be564a8b680865ccc014e6951f78e1c0c Mon Sep 17 00:00:00 2001 From: Jake Huneau Date: Sun, 29 Nov 2026 18:51:53 +0000 Subject: [PATCH 1/4] gh-162597: Mangle a private type parameter used before its definition The symbol table added a generic class's type parameters to the set of names to mangle one at a time, as it visited them. A bound, constraint or default that referred to a later private type parameter was therefore recorded unmangled, while the compiler mangled it with the complete set, found no symbol and evaluated the bound with LOAD_NAME in a scope without locals, raising SystemError. Add all the type parameter names to the set before visiting any of them. --- Lib/test/test_type_params.py | 17 ++++++++++ ...-11-29-18-51-38.gh-issue-162597.T8lkPw.rst | 3 ++ Python/symtable.c | 34 +++++++++++++++++-- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-18-51-38.gh-issue-162597.T8lkPw.rst diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index b6588269e1d0c6..d1ff38e1b31632 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -930,6 +930,23 @@ class Foo[__T, __U: __T](make_base(__T), make_base(lambda: __T)): self.assertIs(base1.__arg__, T) self.assertIs(base2.__arg__(), T) + def test_later_type_params_are_mangled(self): + # gh-162597: a bound, constraint or default that refers to a later + # private type parameter. + ns = run_code(""" + __U = "global" + + class Foo[T: __U, __U, V: (__W, int) = __W, __W = __U, **__P = [__U]]: + pass + """) + Foo = ns["Foo"] + T, U, V, W, P = Foo.__type_params__ + self.assertIs(T.__bound__, U) + self.assertEqual(V.__constraints__, (W, int)) + self.assertIs(V.__default__, W) + self.assertIs(W.__default__, U) + self.assertEqual(P.__default__, [U]) + class TypeParamsComplexCallsTest(unittest.TestCase): def test_defaults(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-18-51-38.gh-issue-162597.T8lkPw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-18-51-38.gh-issue-162597.T8lkPw.rst new file mode 100644 index 00000000000000..31b360712707db --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-18-51-38.gh-issue-162597.T8lkPw.rst @@ -0,0 +1,3 @@ +Fix a :exc:`SystemError` when a bound, constraint or default of a type +parameter of a generic class refers to a later type parameter with a private +name. diff --git a/Python/symtable.c b/Python/symtable.c index 429c204dba29b9..42a655fe0bb53e 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -1664,12 +1664,37 @@ symtable_add_def_ctx(struct symtable *st, PyObject *name, int flag, if ((flag & write_mask) && !check_name(st, name, loc, ctx)) { return 0; } - if ((flag & DEF_TYPE_PARAM) && st->st_cur->ste_mangled_names != NULL) { - if(PySet_Add(st->st_cur->ste_mangled_names, name) < 0) { + return symtable_add_def_helper(st, name, flag, st->st_cur, loc); +} + +/* Add the names of a generic class's type parameters to the set of names + * to mangle before visiting any of them, so that a bound or default that + * refers to a later type parameter is mangled too. */ +static int +symtable_add_mangled_type_params(struct symtable *st, + asdl_type_param_seq *type_params) +{ + for (Py_ssize_t i = 0; i < asdl_seq_LEN(type_params); i++) { + type_param_ty tp = (type_param_ty)asdl_seq_GET(type_params, i); + PyObject *name; + switch (tp->kind) { + case TypeVar_kind: + name = tp->v.TypeVar.name; + break; + case TypeVarTuple_kind: + name = tp->v.TypeVarTuple.name; + break; + case ParamSpec_kind: + name = tp->v.ParamSpec.name; + break; + default: + Py_UNREACHABLE(); + } + if (PySet_Add(st->st_cur->ste_mangled_names, name) < 0) { return 0; } } - return symtable_add_def_helper(st, name, flag, st->st_cur, loc); + return 1; } static int @@ -1986,6 +2011,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) if (!st->st_cur->ste_mangled_names) { return 0; } + if (!symtable_add_mangled_type_params(st, s->v.ClassDef.type_params)) { + return 0; + } VISIT_SEQ(st, type_param, s->v.ClassDef.type_params); } VISIT_SEQ(st, expr, s->v.ClassDef.bases); From 829f1278cad4d04796b0c0246561be590eec9a9e Mon Sep 17 00:00:00 2001 From: Jake Huneau Date: Mon, 30 Nov 2026 12:44:24 +0000 Subject: [PATCH 2/4] Add the reproducer from the issue to the test --- Lib/test/test_type_params.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index d1ff38e1b31632..c169eb714ee871 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -933,6 +933,15 @@ class Foo[__T, __U: __T](make_base(__T), make_base(lambda: __T)): def test_later_type_params_are_mangled(self): # gh-162597: a bound, constraint or default that refers to a later # private type parameter. + class C[T: __U, __U]: + pass + class D[__U, T: __U]: + pass + T, U = C.__type_params__ + self.assertIs(T.__bound__, U) + U, T = D.__type_params__ + self.assertIs(T.__bound__, U) + ns = run_code(""" __U = "global" From f8b46246f7bbc3de616ad533dca0a025f53253ef Mon Sep 17 00:00:00 2001 From: Jake Huneau Date: Mon, 30 Nov 2026 18:45:14 +0000 Subject: [PATCH 3/4] Use run_code for the reproducer --- Lib/test/test_type_params.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index c169eb714ee871..cc52a47e47004b 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -933,13 +933,15 @@ class Foo[__T, __U: __T](make_base(__T), make_base(lambda: __T)): def test_later_type_params_are_mangled(self): # gh-162597: a bound, constraint or default that refers to a later # private type parameter. - class C[T: __U, __U]: - pass - class D[__U, T: __U]: - pass - T, U = C.__type_params__ + ns = run_code(""" + class C[T: __U, __U]: + pass + class D[__U, T: __U]: + pass + """) + T, U = ns["C"].__type_params__ self.assertIs(T.__bound__, U) - U, T = D.__type_params__ + U, T = ns["D"].__type_params__ self.assertIs(T.__bound__, U) ns = run_code(""" From 41256d491b3d288c21aba6ea482863b249c096d3 Mon Sep 17 00:00:00 2001 From: Jake Huneau Date: Tue, 1 Dec 2026 00:45:20 +0000 Subject: [PATCH 4/4] Test a later TypeVarTuple and ParamSpec --- Lib/test/test_type_params.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index cc52a47e47004b..c5af7c56ee93ed 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -938,11 +938,15 @@ class C[T: __U, __U]: pass class D[__U, T: __U]: pass + class E[T: (__Ts, __P), *__Ts, **__P]: + pass """) T, U = ns["C"].__type_params__ self.assertIs(T.__bound__, U) U, T = ns["D"].__type_params__ self.assertIs(T.__bound__, U) + T, Ts, P = ns["E"].__type_params__ + self.assertEqual(T.__constraints__, (Ts, P)) ns = run_code(""" __U = "global"