Skip to content

Commit a86b522

Browse files
authored
bpo-40277: Add a repr() to namedtuple's _tuplegetter to aid with introspection (GH-19537)
1 parent 4f98f46 commit a86b522

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Lib/test/test_collections.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,18 @@ def test_field_doc_reuse(self):
411411
self.assertIs(P.m.__doc__, Q.o.__doc__)
412412
self.assertIs(P.n.__doc__, Q.p.__doc__)
413413

414+
@support.cpython_only
415+
def test_field_repr(self):
416+
Point = namedtuple('Point', 'x y')
417+
self.assertEqual(repr(Point.x), "_tuplegetter(0, 'Alias for field number 0')")
418+
self.assertEqual(repr(Point.y), "_tuplegetter(1, 'Alias for field number 1')")
419+
420+
Point.x.__doc__ = 'The x-coordinate'
421+
Point.y.__doc__ = 'The y-coordinate'
422+
423+
self.assertEqual(repr(Point.x), "_tuplegetter(0, 'The x-coordinate')")
424+
self.assertEqual(repr(Point.y), "_tuplegetter(1, 'The y-coordinate')")
425+
414426
def test_name_fixer(self):
415427
for spec, renamed in [
416428
[('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`collections.namedtuple` now provides a human-readable repr for its
2+
field accessors.

Modules/_collectionsmodule.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,14 @@ tuplegetter_reduce(_tuplegetterobject *self, PyObject *Py_UNUSED(ignored))
24932493
return Py_BuildValue("(O(nO))", (PyObject*) Py_TYPE(self), self->index, self->doc);
24942494
}
24952495

2496+
static PyObject*
2497+
tuplegetter_repr(_tuplegetterobject *self)
2498+
{
2499+
return PyUnicode_FromFormat("%s(%zd, %R)",
2500+
_PyType_Name(Py_TYPE(self)),
2501+
self->index, self->doc);
2502+
}
2503+
24962504

24972505
static PyMemberDef tuplegetter_members[] = {
24982506
{"__doc__", T_OBJECT, offsetof(_tuplegetterobject, doc), 0},
@@ -2515,7 +2523,7 @@ static PyTypeObject tuplegetter_type = {
25152523
0, /* tp_getattr */
25162524
0, /* tp_setattr */
25172525
0, /* tp_as_async */
2518-
0, /* tp_repr */
2526+
(reprfunc)tuplegetter_repr, /* tp_repr */
25192527
0, /* tp_as_number */
25202528
0, /* tp_as_sequence */
25212529
0, /* tp_as_mapping */

0 commit comments

Comments
 (0)