]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40277: Add a repr() to namedtuple's _tuplegetter to aid with introspection (GH...
authorAmmar Askar <ammar@ammaraskar.com>
Wed, 15 Apr 2020 06:36:08 +0000 (23:36 -0700)
committerGitHub <noreply@github.com>
Wed, 15 Apr 2020 06:36:08 +0000 (23:36 -0700)
Lib/test/test_collections.py
Misc/NEWS.d/next/Library/2020-04-14-21-53-18.bpo-40277.NknSaf.rst [new file with mode: 0644]
Modules/_collectionsmodule.c

index 0207823f5aab7262e625ce45799b2dcd3529014a..a8d3337ef5288d18f8756ced408555e0861759ce 100644 (file)
@@ -411,6 +411,18 @@ class TestNamedTuple(unittest.TestCase):
         self.assertIs(P.m.__doc__, Q.o.__doc__)
         self.assertIs(P.n.__doc__, Q.p.__doc__)
 
+    @support.cpython_only
+    def test_field_repr(self):
+        Point = namedtuple('Point', 'x y')
+        self.assertEqual(repr(Point.x), "_tuplegetter(0, 'Alias for field number 0')")
+        self.assertEqual(repr(Point.y), "_tuplegetter(1, 'Alias for field number 1')")
+
+        Point.x.__doc__ = 'The x-coordinate'
+        Point.y.__doc__ = 'The y-coordinate'
+
+        self.assertEqual(repr(Point.x), "_tuplegetter(0, 'The x-coordinate')")
+        self.assertEqual(repr(Point.y), "_tuplegetter(1, 'The y-coordinate')")
+
     def test_name_fixer(self):
         for spec, renamed in [
             [('efg', 'g%hi'),  ('efg', '_1')],                              # field with non-alpha char
diff --git a/Misc/NEWS.d/next/Library/2020-04-14-21-53-18.bpo-40277.NknSaf.rst b/Misc/NEWS.d/next/Library/2020-04-14-21-53-18.bpo-40277.NknSaf.rst
new file mode 100644 (file)
index 0000000..1fa2999
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`collections.namedtuple` now provides a human-readable repr for its
+field accessors.
index 978ea03bd90580ae33882223edceef05e520bf63..7120e4dda0ed236cd99c420e4d483aa40b6b8d9a 100644 (file)
@@ -2493,6 +2493,14 @@ tuplegetter_reduce(_tuplegetterobject *self, PyObject *Py_UNUSED(ignored))
     return Py_BuildValue("(O(nO))", (PyObject*) Py_TYPE(self), self->index, self->doc);
 }
 
+static PyObject*
+tuplegetter_repr(_tuplegetterobject *self)
+{
+    return PyUnicode_FromFormat("%s(%zd, %R)",
+                                _PyType_Name(Py_TYPE(self)),
+                                self->index, self->doc);
+}
+
 
 static PyMemberDef tuplegetter_members[] = {
     {"__doc__",  T_OBJECT, offsetof(_tuplegetterobject, doc), 0},
@@ -2515,7 +2523,7 @@ static PyTypeObject tuplegetter_type = {
     0,                                          /* tp_getattr */
     0,                                          /* tp_setattr */
     0,                                          /* tp_as_async */
-    0,                                          /* tp_repr */
+    (reprfunc)tuplegetter_repr,                 /* tp_repr */
     0,                                          /* tp_as_number */
     0,                                          /* tp_as_sequence */
     0,                                          /* tp_as_mapping */