Skip to content

Commit

Permalink
Fix bug in IndexedDict.__str__ in Python 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mlenzen committed Nov 24, 2019
1 parent cf98969 commit 3b5c5dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 12 additions & 2 deletions collections_extended/indexed_dict.py
Expand Up @@ -305,9 +305,19 @@ def __repr__(self):
)

def __str__(self):
return "{class_name}({data})".format(
# When Python 3.5 support is dropped, we can rely on dict order and this
# can be simplified to:
# return "{class_name}({data})".format(
# class_name=self.__class__.__name__,
# data=repr(dict(self)),
# )
data = ', '.join(
'{k!r}: {v!r}'.format(k=k, v=v)
for k, v in self.items()
)
return "{class_name}({{{data}}})".format(
class_name=self.__class__.__name__,
data=repr({k: self[k] for k in self}),
data=data,
)

def __getitem__(self, key):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_indexed_dict.py
Expand Up @@ -383,3 +383,7 @@ def test_str():
d["Y"] = 2
d[None] = None
assert str(d) == "IndexedDict({1: 'X', 'Y': 2, None: None})"


def test_items(d):
assert list(d.items()) == [(chr(ord("a") + i), 10 + i) for i in range(5)]

0 comments on commit 3b5c5dd

Please sign in to comment.