Skip to content

Commit f7e3ea8

Browse files
idanw206cjw296
authored andcommitted
bpo-42532: Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function (GH23613)
Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function Backports: c598a04dd29b89ad072245ddaf738badcfb41ac7 Signed-off-by: Chris Withers <[email protected]>
1 parent e5d7551 commit f7e3ea8

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove unexpected call of ``__bool__`` when passing a ``spec_arg`` argument to a Mock.

mock/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def __new__(cls, *args, **kw):
408408
# Check if spec is an async object or function
409409
bound_args = _MOCK_SIG.bind_partial(cls, *args, **kw).arguments
410410
spec_arg = bound_args.get('spec_set', bound_args.get('spec'))
411-
if spec_arg and _is_async_obj(spec_arg):
411+
if spec_arg is not None and _is_async_obj(spec_arg):
412412
bases = (AsyncMockMixin, cls)
413413
new = type(cls.__name__, bases, {'__doc__': cls.__doc__})
414414
instance = _safe_super(NonCallableMock, cls).__new__(new)

mock/tests/testmock.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,16 @@ def trace(frame, event, arg): # pragma: no cover
21672167
obj = mock(spec=Something)
21682168
self.assertIsInstance(obj, Something)
21692169

2170+
def test_bool_not_called_when_passing_spec_arg(self):
2171+
class Something:
2172+
def __init__(self):
2173+
self.obj_with_bool_func = unittest.mock.MagicMock()
2174+
2175+
obj = Something()
2176+
with unittest.mock.patch.object(obj, 'obj_with_bool_func', autospec=True): pass
2177+
2178+
self.assertEqual(obj.obj_with_bool_func.__bool__.call_count, 0)
2179+
21702180

21712181
if __name__ == '__main__':
21722182
unittest.main()

0 commit comments

Comments
 (0)