diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index b6588269e1d0c6..cc52a47e47004b 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -930,6 +930,34 @@ 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(""" + 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 = ns["D"].__type_params__ + self.assertIs(T.__bound__, U) + + 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);