Skip to content
Closed
Prev Previous commit
Allow defaultdict to take care of missing event objects
  • Loading branch information
tirkarthi committed Apr 14, 2019
commit 5b59f340d99d092a1cac973ccae8784a39682e4b
8 changes: 1 addition & 7 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,6 @@ class WaitableMock(MagicMock):

def __init__(self, *args, event_class=threading.Event, **kwargs):
_safe_super(WaitableMock, self).__init__(*args, **kwargs)
self._event_class = event_class
self._event = event_class()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage self._event vs self._expected_calls which contains events is non obvious. Can add a comment to explain where each event is used and how.

self._expected_calls = defaultdict(event_class)

Expand All @@ -2522,12 +2521,7 @@ def wait_until_called_with(self, *args, timeout=1.0):

`timeout` - time to wait for in seconds. Defaults to 1.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t have a great alternative, but feels a pity if users cannot wait for calls that had a timeout parameter as discussed in the issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I would wait if someone has a less conflicting name or perhaps receive kwargs_dict as a dictionary instead of (**kwargs, timeout) that fixes conflict but the API would be nice to pass keyword arguments as **kwargs itself.

"""
if args not in self._expected_calls:
event = self._event_class()
self._expected_calls[args] = event
else:
event = self._expected_calls[args]

event = self._expected_calls[args]
return event.wait(timeout=timeout)


Expand Down