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

Skip to content

Commit bd49622

Browse files
committed
Handle name mangling
1 parent 394d128 commit bd49622

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Lib/test/test_type_params.py

+23
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,29 @@ def foo[T: glb, U: cls](self): ...
341341
exec(code, {})
342342

343343

344+
class ManglingTest(unittest.TestCase):
345+
def test_mangling(self):
346+
class Foo[__T]:
347+
param = __T
348+
def meth[__U](self, arg: __T, arg2: __U):
349+
return (__T, __U)
350+
type Alias[__V] = (__T, __V)
351+
352+
T = Foo.__type_params__[0]
353+
self.assertEqual(T.__name__, "__T")
354+
U = Foo.meth.__type_params__[0]
355+
self.assertEqual(U.__name__, "__U")
356+
V = Foo.Alias.__type_params__[0]
357+
self.assertEqual(V.__name__, "__V")
358+
359+
anno = Foo.meth.__annotations__
360+
self.assertIs(anno["arg"], T)
361+
self.assertIs(anno["arg2"], U)
362+
self.assertEqual(Foo().meth(1, 2), (T, U))
363+
364+
self.assertEqual(Foo.Alias.__value__, (T, V))
365+
366+
344367
class TypeParamsTraditionalTypeVars(unittest.TestCase):
345368
def test_traditional_01(self):
346369
code = textwrap.dedent("""\

Python/compile.c

+1
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,7 @@ compiler_class(struct compiler *c, stmt_ty s)
23392339

23402340
asdl_typeparam_seq *typeparams = s->v.ClassDef.typeparams;
23412341
if (asdl_seq_LEN(typeparams) > 0) {
2342+
Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
23422343
ADDOP(c, loc, PUSH_NULL);
23432344
PyObject *typeparams_name = PyUnicode_FromFormat("<generic parameters of %U>",
23442345
s->v.ClassDef.name);

Python/symtable.c

+1
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ symtable_enter_typeparam_block(struct symtable *st, identifier name,
11931193
}
11941194
}
11951195
if (kind == ClassDef_kind) {
1196+
st->st_private = name;
11961197
// This is used for setting the generic base
11971198
_Py_DECLARE_STR(generic_base, ".generic_base");
11981199
if (!symtable_add_def(st, &_Py_STR(generic_base), DEF_LOCAL,

0 commit comments

Comments
 (0)