Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make __repr__ more similar to other mapping types #58

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions immutables/_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -3194,14 +3194,14 @@ map_py_repr(BaseMapObject *m)

if (MapMutation_Check(m)) {
if (_PyUnicodeWriter_WriteASCIIString(
&writer, "<immutables.MapMutation({", 25) < 0)
&writer, "immutables.MapMutation({", 24) < 0)
{
goto error;
}
}
else {
if (_PyUnicodeWriter_WriteASCIIString(
&writer, "<immutables.Map({", 17) < 0)
&writer, "immutables.Map({", 16) < 0)
{
goto error;
}
Expand Down Expand Up @@ -3255,16 +3255,6 @@ map_py_repr(BaseMapObject *m)
goto error;
}

PyObject *addr = PyUnicode_FromFormat(" at %p>", m);
if (addr == NULL) {
goto error;
}
if (_PyUnicodeWriter_WriteStr(&writer, addr) < 0) {
Py_DECREF(addr);
goto error;
}
Py_DECREF(addr);

Py_ReprLeave((PyObject *)m);
return _PyUnicodeWriter_Finish(&writer);

Expand Down
6 changes: 2 additions & 4 deletions immutables/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,7 @@ def __repr__(self):
items = []
for key, val in self.items():
items.append("{!r}: {!r}".format(key, val))
return '<immutables.Map({{{}}}) at 0x{:0x}>'.format(
', '.join(items), id(self))
return 'immutables.Map({{{}}})'.format(', '.join(items))

def __dump__(self): # pragma: no cover
buf = []
Expand Down Expand Up @@ -818,8 +817,7 @@ def __repr__(self):
items = []
for key, val in self.__root.items():
items.append("{!r}: {!r}".format(key, val))
return '<immutables.MapMutation({{{}}}) at 0x{:0x}>'.format(
', '.join(items), id(self))
return 'immutables.MapMutation({{{}}})'.format(', '.join(items))

def __len__(self):
return self.__count
Expand Down
11 changes: 4 additions & 7 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,10 @@ def test_map_getitem_1(self):

def test_repr_1(self):
h = self.Map()
self.assertTrue(repr(h).startswith('<immutables.Map({}) at 0x'))
self.assertEqual(repr(h), 'immutables.Map({})')

h = h.set(1, 2).set(2, 3).set(3, 4)
self.assertTrue(repr(h).startswith(
'<immutables.Map({1: 2, 2: 3, 3: 4}) at 0x'))
self.assertEqual(repr(h), 'immutables.Map({1: 2, 2: 3, 3: 4})')

def test_repr_2(self):
h = self.Map()
Expand Down Expand Up @@ -879,8 +878,7 @@ def __repr__(self):
h = h.set(k, 1)
k.val = h

self.assertTrue(repr(h).startswith(
'<immutables.Map({{...}: 1}) at 0x'))
self.assertEqual(repr(h), 'immutables.Map({{...}: 1})')

def test_hash_1(self):
h = self.Map()
Expand Down Expand Up @@ -964,8 +962,7 @@ def test_map_mut_3(self):
h = h.set('a', 1)
hm1 = h.mutate()

self.assertTrue(repr(hm1).startswith(
"<immutables.MapMutation({'a': 1})"))
self.assertEqual(repr(hm1), "immutables.MapMutation({'a': 1})")

with self.assertRaisesRegex(TypeError, 'unhashable type'):
hash(hm1)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_none_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_none_as_key(self):
self.assertEqual(len(m), 1)
self.assertTrue(None in m)
self.assertEqual(m[None], 1)
self.assertTrue(repr(m).startswith('<immutables.Map({None: 1}) at 0x'))
self.assertEqual(repr(m), 'immutables.Map({None: 1})')

for level in range(7):
key = NoneCollision('a', level)
Expand All @@ -72,7 +72,7 @@ def test_none_as_key(self):
m = m.delete(None)
self.assertEqual(len(m), 0)
self.assertFalse(None in m)
self.assertTrue(repr(m).startswith('<immutables.Map({}) at 0x'))
self.assertEqual(repr(m), 'immutables.Map({})')

self.assertEqual(m, self.Map())

Expand Down Expand Up @@ -125,7 +125,7 @@ def test_none_collision_1(self):
self.assertFalse(None in m3)
self.assertFalse(key in m3)
self.assertEqual(m3, self.Map())
self.assertTrue(repr(m3).startswith('<immutables.Map({}) at 0x'))
self.assertEqual(repr(m3), 'immutables.Map({})')
with self.assertRaises(KeyError):
m3.delete(None)
with self.assertRaises(KeyError):
Expand All @@ -144,7 +144,7 @@ def test_none_collision_1(self):
self.assertFalse(None in m4)
self.assertFalse(key in m4)
self.assertEqual(m4, self.Map())
self.assertTrue(repr(m4).startswith('<immutables.Map({}) at 0x'))
self.assertEqual(repr(m4), 'immutables.Map({})')
with self.assertRaises(KeyError):
m4.delete(None)
with self.assertRaises(KeyError):
Expand Down