Skip to content

Commit 07df123

Browse files
committed
Ensure the constructor works with unhashable objects
1 parent a36a036 commit 07df123

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

mockito/inorder.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# THE SOFTWARE.
2020
from __future__ import annotations
2121

22-
from collections import Counter, deque
22+
from collections import deque
2323
from functools import partial
2424
from typing import Deque
2525

@@ -41,14 +41,12 @@ def verify(object, *args, **kwargs):
4141
class InOrder:
4242

4343
def __init__(self, *objects: object):
44-
counter = Counter(objects)
45-
duplicates = [d for d, freq in counter.items() if freq > 1]
46-
if duplicates:
47-
raise ValueError(
48-
f"\nThe following Mocks are duplicated: "
49-
f"{[str(d) for d in duplicates]}"
50-
)
51-
self._objects = objects
44+
objects_ = []
45+
for obj in objects:
46+
if obj in objects_:
47+
raise ValueError(f"{obj} is provided more than once")
48+
objects_.append(obj)
49+
self._objects = objects_
5250
self._attach_all()
5351
self.ordered_invocations: Deque[RealInvocation] = deque()
5452

tests/in_order_test.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ def test_observing_the_same_mock_twice_should_raise():
1717
a = mock()
1818
with pytest.raises(ValueError) as e:
1919
InOrder(a, a)
20-
assert str(e.value) == ("\nThe following Mocks are duplicated: "
21-
f"['{a}']")
20+
assert str(e.value) == f"{a} is provided more than once"
21+
22+
23+
def test_observing_the_same_mock_twice_should_raise_unhashable_obj():
24+
a = dict() # type: ignore[var-annotated]
25+
with pytest.raises(ValueError):
26+
InOrder(a, a)
27+
2228

2329
def test_correct_order_declaration_should_pass():
2430
cat = mock()

0 commit comments

Comments
 (0)