Skip to content

Commit a7e7a21

Browse files
Carl MeyerService User
Carl Meyer
authored and
Service User
committed
[unittest.mock] restore Mock._spec_asyncs and match upstream
Summary: D42039568 backported an early version of python/cpython#100252, and there were some changes made before that PR was merged upstream. Backport those changes so we match upstream behavior. Most importantly, this restores `_spec_asyncs`, which although a private API does seem to be used. Test Plan: Test suite Reviewers: itamaro, #cinder Reviewed By: itamaro Subscribers: mpage, jackyzhang Differential Revision: https://phabricator.intern.facebook.com/D42416767 Tasks: T141890380 Tags: commitClose, cinder-310-upstream-complete, publish_when_ready
1 parent 80b52b4 commit a7e7a21

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Lib/unittest/mock.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
493493
_eat_self=False):
494494
_spec_class = None
495495
_spec_signature = None
496+
_spec_asyncs = []
496497

497498
if spec is not None and not _is_list(spec):
498499
if isinstance(spec, type):
@@ -503,13 +504,20 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
503504
_spec_as_instance, _eat_self)
504505
_spec_signature = res and res[1]
505506

506-
spec = dir(spec)
507+
spec_list = dir(spec)
508+
509+
for attr in spec_list:
510+
if iscoroutinefunction(getattr(spec, attr, None)):
511+
_spec_asyncs.append(attr)
512+
513+
spec = spec_list
507514

508515
__dict__ = self.__dict__
509516
__dict__['_spec_class'] = _spec_class
510517
__dict__['_spec_set'] = spec_set
511518
__dict__['_spec_signature'] = _spec_signature
512519
__dict__['_mock_methods'] = spec
520+
__dict__['_spec_asyncs'] = _spec_asyncs
513521

514522
def __get_return_value(self):
515523
ret = self._mock_return_value
@@ -998,8 +1006,7 @@ def _get_child_mock(self, /, **kw):
9981006
For non-callable mocks the callable variant will be used (rather than
9991007
any custom subclass)."""
10001008
_new_name = kw.get("_new_name")
1001-
_spec_val = getattr(self.__dict__["_spec_class"], _new_name, None)
1002-
if _spec_val is not None and asyncio.iscoroutinefunction(_spec_val):
1009+
if _new_name in self.__dict__['_spec_asyncs']:
10031010
return AsyncMock(**kw)
10041011

10051012
if self._mock_sealed:

0 commit comments

Comments
 (0)