Skip to content

Commit

Permalink
Add NormalizedDict.__repr__. Fixes #4709.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed Mar 30, 2023
1 parent 70f20be commit fa20cd4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/robot/utils/normalizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ def __len__(self):
return len(self._data)

def __str__(self):
return '{%s}' % ', '.join('%r: %r' % (key, self[key]) for key in self)
items = ', '.join(f'{key!r}: {self[key]!r}' for key in self)
return f'{{{items}}}'

def __repr__(self):
name = type(self).__name__
params = str(self) if self else ''
return f'{name}({params})'

def __eq__(self, other):
if not is_dict_like(other):
Expand Down
10 changes: 8 additions & 2 deletions utest/utils/test_normalizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,16 @@ def test_copy(self):
assert_equal(cd._data, {'a': 1, 'b': 2})

def test_str(self):
nd = NormalizedDict({'a': 1, 'B': 1, 'c': 3, 'd': 4, 'E': 5, 'F': 6})
expected = "{'a': 1, 'B': 1, 'c': 3, 'd': 4, 'E': 5, 'F': 6}"
nd = NormalizedDict({'a': 1, 'B': 2, 'c': '3', 'd': '"', 'E': 5, 'F': 6})
expected = "{'a': 1, 'B': 2, 'c': '3', 'd': '\"', 'E': 5, 'F': 6}"
assert_equal(str(nd), expected)

def test_repr(self):
assert_equal(repr(NormalizedDict()), 'NormalizedDict()')
assert_equal(repr(NormalizedDict({'a': None, 'b': '"', 'A': 1})),
"NormalizedDict({'a': 1, 'b': '\"'})")
assert_equal(repr(type('Extend', (NormalizedDict,), {})()), 'Extend()')

def test_unicode(self):
nd = NormalizedDict({'a': '\xe4', '\xe4': 'a'})
assert_equal(str(nd), "{'a': '\xe4', '\xe4': 'a'}")
Expand Down

0 comments on commit fa20cd4

Please sign in to comment.