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

Skip to content

Commit f7e6219

Browse files
committed
Simplify TypeAliasType.__repr__
1 parent a1c59f7 commit f7e6219

2 files changed

Lines changed: 4 additions & 113 deletions

File tree

Lib/test/test_type_aliases.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,38 +140,12 @@ def test_subscripting(self):
140140

141141
def test_repr(self):
142142
type Simple = int
143-
self.assertEqual(repr(Simple), "<type alias Simple: int>")
144-
145-
class FancyRepr:
146-
def __repr__(self):
147-
return "<fancy>"
148-
type Fancy = FancyRepr()
149-
self.assertEqual(repr(Fancy), "<type alias Fancy: <fancy>>")
150-
151-
class FancyMeta(type):
152-
def __repr__(self):
153-
return f"<fancy class {self.__name__!r}>"
154-
class FancyCls(metaclass=FancyMeta):
155-
pass
156-
type Fancy2 = FancyCls
157-
self.assertEqual(repr(Fancy2), "<type alias Fancy2: <fancy class 'FancyCls'>>")
158-
159-
type NoRepr = 1 / 0
160-
self.assertEqual(repr(NoRepr), "<type alias NoRepr: <evaluation failed>>")
161-
162-
class FailingRepr:
163-
def __repr__(self):
164-
raise ValueError("nope")
165-
type Failing = FailingRepr()
166-
self.assertEqual(repr(Failing), "<type alias Failing: <value repr() failed>>")
167-
168-
type Generic[T, *Ts, **P] = int
169-
self.assertEqual(repr(Generic), "<type alias Generic[T, *Ts, **P]: int>")
143+
self.assertEqual(repr(Simple), "Simple")
170144

171145
def test_recursive_repr(self):
172146
type Recursive = Recursive
173-
self.assertEqual(repr(Recursive), "<type alias Recursive: ...>")
147+
self.assertEqual(repr(Recursive), "Recursive")
174148

175149
type X = list[Y]
176150
type Y = list[X]
177-
self.assertEqual(repr(X), "<type alias X: list[<type alias Y: list[...]>]>")
151+
self.assertEqual(repr(X), "X")

Objects/typevarobject.c

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,91 +1289,8 @@ typealias_get_value(typealiasobject *ta)
12891289
static PyObject *
12901290
typealias_repr(PyObject *self)
12911291
{
1292-
Py_ssize_t res = Py_ReprEnter(self);
1293-
if (res > 0) {
1294-
return PyUnicode_FromString("...");
1295-
}
1296-
else if (res < 0) {
1297-
return NULL;
1298-
}
1299-
13001292
typealiasobject *ta = (typealiasobject *)self;
1301-
PyObject *value_repr = NULL;
1302-
PyObject *value = typealias_get_value(ta);
1303-
if (value == NULL) {
1304-
PyErr_Clear();
1305-
value_repr = PyUnicode_FromString("<evaluation failed>");
1306-
}
1307-
// Show "int" instead of "<class 'int'>"
1308-
else if (PyType_Check(value) && Py_TYPE(value)->tp_repr == PyType_Type.tp_repr
1309-
&& ((PyTypeObject *)value)->tp_name != NULL) {
1310-
value_repr = PyUnicode_FromString(((PyTypeObject *)value)->tp_name);
1311-
Py_DECREF(value);
1312-
}
1313-
else {
1314-
value_repr = PyObject_Repr(value);
1315-
Py_DECREF(value);
1316-
if (value_repr == NULL) {
1317-
PyErr_Clear();
1318-
value_repr = PyUnicode_FromString("<value repr() failed>");
1319-
}
1320-
}
1321-
if (value_repr == NULL) {
1322-
// PyUnicode_FromString failed
1323-
Py_ReprLeave(self);
1324-
return NULL;
1325-
}
1326-
PyObject *result = NULL;
1327-
if (ta->type_params != NULL) {
1328-
_PyUnicodeWriter writer;
1329-
_PyUnicodeWriter_Init(&writer);
1330-
PyTupleObject *type_params = (PyTupleObject *)ta->type_params;
1331-
Py_ssize_t count = PyTuple_GET_SIZE(type_params);
1332-
PyInterpreterState *interp = PyInterpreterState_Get();
1333-
for (Py_ssize_t i = 0; i < count; i++) {
1334-
PyObject *type_param = PyTuple_GET_ITEM(type_params, i);
1335-
if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
1336-
_PyUnicodeWriter_Dealloc(&writer);
1337-
goto error;
1338-
}
1339-
if (Py_IS_TYPE(type_param, interp->cached_objects.paramspec_type)) {
1340-
if (_PyUnicodeWriter_WriteASCIIString(&writer, "**", 2) < 0) {
1341-
_PyUnicodeWriter_Dealloc(&writer);
1342-
goto error;
1343-
}
1344-
}
1345-
else if (Py_IS_TYPE(type_param, interp->cached_objects.typevartuple_type)) {
1346-
if (_PyUnicodeWriter_WriteASCIIString(&writer, "*", 1) < 0) {
1347-
_PyUnicodeWriter_Dealloc(&writer);
1348-
goto error;
1349-
}
1350-
}
1351-
PyObject *type_param_repr = PyObject_Repr(type_param);
1352-
if (type_param_repr == NULL) {
1353-
_PyUnicodeWriter_Dealloc(&writer);
1354-
goto error;
1355-
}
1356-
if (_PyUnicodeWriter_WriteStr(&writer, type_param_repr) < 0) {
1357-
Py_DECREF(type_param_repr);
1358-
_PyUnicodeWriter_Dealloc(&writer);
1359-
goto error;
1360-
}
1361-
Py_DECREF(type_param_repr);
1362-
}
1363-
PyObject *params_repr = _PyUnicodeWriter_Finish(&writer);
1364-
if (params_repr == NULL) {
1365-
goto error;
1366-
}
1367-
result = PyUnicode_FromFormat("<type alias %s[%U]: %U>", ta->name, params_repr, value_repr);
1368-
Py_DECREF(params_repr);
1369-
}
1370-
else {
1371-
result = PyUnicode_FromFormat("<type alias %s: %U>", ta->name, value_repr);
1372-
}
1373-
error:
1374-
Py_ReprLeave(self);
1375-
Py_DECREF(value_repr);
1376-
return result;
1293+
return PyUnicode_FromString(ta->name);
13771294
}
13781295

13791296
static PyMemberDef typealias_members[] = {

0 commit comments

Comments
 (0)