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

bpo-29403: Fix mock's broken autospec behavior on method-bound builtin functions #3

Merged
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
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _set_signature(mock, original, instance=False):
skipfirst = isinstance(original, type)
result = _get_signature_object(original, instance, skipfirst)
if result is None:
return
return mock
func, sig = result
def checksig(*args, **kwargs):
sig.bind(*args, **kwargs)
Expand Down
15 changes: 15 additions & 0 deletions Lib/unittest/test/testmock/testhelpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time
import types
import unittest

from unittest.mock import (
Expand Down Expand Up @@ -856,6 +858,19 @@ def check_data_descriptor(mock_attr):
check_data_descriptor(foo.desc)


def test_autospec_on_bound_builtin_function(self):
meth = types.MethodType(time.ctime, time.time())
self.assertIsInstance(meth(), str)
mocked = create_autospec(meth)

# no signature, so no spec to check against
mocked()
mocked.assert_called_once_with()
mocked.reset_mock()
mocked(4, 5, 6)
mocked.assert_called_once_with(4, 5, 6)


class TestCallList(unittest.TestCase):

def test_args_list_contains_call_list(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``unittest.mock``'s autospec to not fail on method-bound builtin
functions. Patch by Aaron Gallagher.