Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 585 steps 7-8: Implement repr for GenericAlias #1

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class MyList(list):
self.assertIs(t.__origin__, MyList)
self.assertEqual(t.__parameters__, (int,))

def test_repr(self):
class MyList(list):
pass
self.assertEqual(repr(list[str]), 'list[str]')
self.assertEqual(repr(list[()]), 'list[()]')
self.assertEqual(repr(MyList[int]), 'test.test_genericalias.BaseTest.test_repr.<locals>.MyList[int]')
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(repr(list[str]()), '[]') # instances should keep their normal repr
ethanhs marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
unittest.main()
67 changes: 65 additions & 2 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1821,12 +1821,75 @@ ga_traverse(PyObject *self, visitproc visit, void *arg)
return 0;
}

// TODO: nicely format origin and each parameter
static PyObject*
ga_repr_item(PyObject *p)
{
PyObject *qualname = PyObject_GetAttrString(p, "__qualname__");
PyObject *module = PyObject_GetAttrString(p, "__module__");
PyObject *r = NULL;

if (PyObject_HasAttrString(p, "__origin__") &&
PyObject_HasAttrString(p, "__parameters__"))
{
// It looks like a GenericAlias
r = PyObject_Repr(p);
}
else if (p == Py_Ellipsis) {
// The Ellipsis object
r = PyUnicode_FromString("...");
}
else if (qualname != NULL && module != NULL) {
// Looks like a class
if (PyUnicode_CompareWithASCIIString(module, "builtins") == 0) {
// builtins don't need a module name
r = qualname;
Py_DECREF(module);
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
}
else {
r = PyUnicode_FromFormat("%U.%U", module, qualname);
Py_DECREF(qualname);
Py_DECREF(module);
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
}
}
else {
// fallback
r = PyObject_Repr(p);
}
return r;
}

static PyObject *
ga_repr(PyObject *self)
{
gaobject *alias = (gaobject *)self;
return PyUnicode_FromFormat("%R[%R]", alias->origin, alias->parameters);
PyObject *origin = NULL;
PyObject *reprs = NULL;
PyObject *params = NULL;
PyObject *alias_repr = NULL;
PyObject *sep = PyUnicode_FromString(", ");
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
Py_ssize_t len = PyTuple_Size(alias->parameters);

reprs = PyList_New(len);
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
for (Py_ssize_t i = 0; i < len; i++) {
PyObject *p = PyTuple_GET_ITEM(alias->parameters, i);
PyList_SET_ITEM(reprs, i, ga_repr_item(p));
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
}

origin = ga_repr_item(alias->origin);
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
if (len == 0) {
// for something like tuple[()] we should print a "()"
int err = PyList_Append(reprs, PyUnicode_FromString("()"));
if (err == -1)
return NULL;
}
params = PyUnicode_Join(sep, reprs);
ethanhs marked this conversation as resolved.
Show resolved Hide resolved
alias_repr = PyUnicode_FromFormat("%U[%U]", origin, params);

Py_DECREF(origin);
Py_DECREF(reprs);
Py_DECREF(params);
Py_DECREF(sep);
return alias_repr;
}

static PyObject *
Expand Down