Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f1182cd

Browse files
committed
Merge
2 parents 748d7ae + 4e6bf41 commit f1182cd

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

Lib/collections/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,12 @@ def __delitem__(self, elem):
585585
def __repr__(self):
586586
if not self:
587587
return '%s()' % self.__class__.__name__
588-
items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
589-
return '%s({%s})' % (self.__class__.__name__, items)
588+
try:
589+
items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
590+
return '%s({%s})' % (self.__class__.__name__, items)
591+
except TypeError:
592+
# handle case where values are not orderable
593+
return '{0}({1!r})'.format(self.__class__.__name__, dict(self))
590594

591595
# Multiset-style mathematical operations discussed in:
592596
# Knuth TAOCP Volume II section 4.6.3 exercise 19

Lib/test/test_collections.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,12 @@ def test_unary(self):
969969
self.assertEqual(dict(+c), dict(c=5, d=10, e=15, g=40))
970970
self.assertEqual(dict(-c), dict(a=5))
971971

972+
def test_repr_nonsortable(self):
973+
c = Counter(a=2, b=None)
974+
r = repr(c)
975+
self.assertIn("'a': 2", r)
976+
self.assertIn("'b': None", r)
977+
972978
def test_helper_function(self):
973979
# two paths, one for real dicts and one for other mappings
974980
elems = list('abracadabra')

0 commit comments

Comments
 (0)