[Python-checkins] r83808 - in python/branches/py3k: Doc/library/collections.rst Lib/collections.py Lib/test/test_collections.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Sun Aug 8 03:13:42 CEST 2010


Author: raymond.hettinger
Date: Sun Aug  8 03:13:42 2010
New Revision: 83808

Log:
Issue #9507:  Named tuple repr will now automatically display the right
name in a tuple subclass.



Modified:
   python/branches/py3k/Doc/library/collections.rst
   python/branches/py3k/Lib/collections.py
   python/branches/py3k/Lib/test/test_collections.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Doc/library/collections.rst
==============================================================================
--- python/branches/py3k/Doc/library/collections.rst	(original)
+++ python/branches/py3k/Doc/library/collections.rst	Sun Aug  8 03:13:42 2010
@@ -605,7 +605,7 @@
    <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'

Modified: python/branches/py3k/Lib/collections.py
==============================================================================
--- python/branches/py3k/Lib/collections.py	(original)
+++ python/branches/py3k/Lib/collections.py	Sun Aug  8 03:13:42 2010
@@ -240,7 +240,7 @@
             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

Modified: python/branches/py3k/Lib/test/test_collections.py
==============================================================================
--- python/branches/py3k/Lib/test/test_collections.py	(original)
+++ python/branches/py3k/Lib/test/test_collections.py	Sun Aug  8 03:13:42 2010
@@ -218,6 +218,16 @@
         # 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):

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Aug  8 03:13:42 2010
@@ -24,6 +24,9 @@
 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.
 


More information about the Python-checkins mailing list