8000 Fix repr() for recursive type aliases · python/cpython@27a8a4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 27a8a4c

Browse files
committed
Fix repr() for recursive type aliases
1 parent d32bc61 commit 27a8a4c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_type_aliases.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,11 @@ def __repr__(self):
127127

128128
type Generic[T, *Ts, **P] = int
129129
self.assertEqual(repr(Generic), "<type alias Generic[T, *Ts, **P]: int>")
130+
131+
def test_recursive_repr(self):
132+
type Recursive = Recursive
133+
self.assertEqual(repr(Recursive), "<type alias Recursive: ...>")
134+
135+
type X = list[Y]
136+
type Y = list[X]
137+
self.assertEqual(repr(X), "<type alias X: list[<type alias Y: list[...]>]>")

Objects/typevarobject.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,14 @@ typealias_evaluate(typealiasobject *ta)
12491249
static PyObject *
12501250
typealias_repr(PyObject *self)
12511251
{
1252+
Py_ssize_t res = Py_ReprEnter(self);
1253+
if (res > 0) {
1254+
return PyUnicode_FromString("...");
1255+
}
1256+
else if (res < 0) {
1257+
return NULL;
1258+
}
1259+
12521260
typealiasobject *ta = (typealiasobject *)self;
12531261
PyObject *value_repr = NULL;
12541262
PyObject *value = typealias_evaluate(ta);
@@ -1271,6 +1279,7 @@ typealias_repr(PyObject *self)
12711279
}
12721280
if (value_repr == NULL) {
12731281
// PyUnicode_FromString failed
1282+
Py_ReprLeave(self);
12741283
return NULL;
12751284
}
12761285
PyObject *result = NULL;
@@ -1321,6 +1330,7 @@ typealias_repr(PyObject *self)
13211330
result = PyUnicode_FromFormat("<type alias %s: %U>", ta->name, value_repr);
13221331
}
13231332
error:
1333+
Py_ReprLeave(self);
13241334
Py_DECREF(value_repr);
13251335
return result;
13261336
}

0 commit comments

Comments
 (0)
0