bpo-17185: Add __signature__ to mock that can be used by inspect for … · python/cpython@f7fa62e

Original file line numberDiff line numberDiff line change

@@ -1,3 +1,4 @@

1+

import inspect

12

import time

23

import types

34

import unittest

@@ -901,6 +902,35 @@ def __getattr__(self, attribute):

901902

self.assertFalse(hasattr(autospec, '__name__'))

902903
903904
905+

def test_spec_inspect_signature(self):

906+
907+

def myfunc(x, y):

908+

pass

909+
910+

mock = create_autospec(myfunc)

911+

mock(1, 2)

912+

mock(x=1, y=2)

913+
914+

self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(myfunc))

915+

self.assertEqual(mock.mock_calls, [call(1, 2), call(x=1, y=2)])

916+

self.assertRaises(TypeError, mock, 1)

917+
918+
919+

def test_spec_inspect_signature_annotations(self):

920+
921+

def foo(a: int, b: int=10, *, c:int) -> int:

922+

return a + b + c

923+
924+

mock = create_autospec(foo)

925+

mock(1, 2, c=3)

926+

mock(1, c=3)

927+
928+

self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(foo))

929+

self.assertEqual(mock.mock_calls, [call(1, 2, c=3), call(1, c=3)])

930+

self.assertRaises(TypeError, mock, 1)

931+

self.assertRaises(TypeError, mock, 1, 2, 3, c=4)

932+
933+
904934

class TestCallList(unittest.TestCase):

905935
906936

def test_args_list_contains_call_list(self):