summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-08-08 01:13:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-08-08 01:13:42 (GMT)
commitd331ce9e6686f180373853fef5d819f36d331d44 (patch)
tree56c8cc04e49237168a85545285f9cbcacf02ad55
parenta6b76ba52e00c47eafba57362184f7779330154a (diff)
downloadcpython-d331ce9e6686f180373853fef5d819f36d331d44.zip
cpython-d331ce9e6686f180373853fef5d819f36d331d44.tar.gz
cpython-d331ce9e6686f180373853fef5d819f36d331d44.tar.bz2
Issue #9507: Named tuple repr will now automatically display the right
name in a tuple subclass.
-rw-r--r--Doc/library/collections.rst2
-rw-r--r--Lib/collections.py2
-rw-r--r--Lib/test/test_collections.py10
-rw-r--r--Misc/NEWS3
4 files changed, 15 insertions, 2 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 741c7a0..66d373d 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -605,7 +605,7 @@ Example:
<BLANKLINE>
def __repr__(self):
'Return a nicely formatted representation string'
- return 'Point(x=%r, y=%r)' % self
+ return self.__class__.__name__ + '(x=%r, y=%r)' % self
<BLANKLINE>
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
diff --git a/Lib/collections.py b/Lib/collections.py
index 2ce46de..1a43afb 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -240,7 +240,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
return result \n
def __repr__(self):
'Return a nicely formatted representation string'
- return '%(typename)s(%(reprtxt)s)' %% self \n
+ return self.__class__.__name__ + '(%(reprtxt)s)' %% self \n
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self)) \n
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 69c4a9f..2af94bf 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -218,6 +218,16 @@ class TestNamedTuple(unittest.TestCase):
# test __getnewargs__
self.assertEqual(t.__getnewargs__(), values)
+ def test_repr(self):
+ with support.captured_stdout() as template:
+ A = namedtuple('A', 'x', verbose=True)
+ self.assertEqual(repr(A(1)), 'A(x=1)')
+ # repr should show the name of the subclass
+ class B(A):
+ pass
+ self.assertEqual(repr(B(1)), 'B(x=1)')
+
+
class ABCTestCase(unittest.TestCase):
def validate_abstract_methods(self, abc, *names):
diff --git a/Misc/NEWS b/Misc/NEWS
index 4f99d70..c0e3fc7 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
Extensions
----------
+- Issue #9507: Named tuple repr will now automatically display the right
+ name in a tuple subclass.
+
- Issue #9324: Add parameter validation to signal.signal on Windows in order
to prevent crashes.