From: Berker Peksag Date: Thu, 12 Mar 2015 18:42:48 +0000 (+0200) Subject: Issue #23581: Add matmul support to MagicMock. X-Git-Tag: v3.5.0a3~194 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9bd8af788d4f788ae678898b5edff035fe2b9a8e;p=python Issue #23581: Add matmul support to MagicMock. Patch by Håkan Lövdahl. --- diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 3b7c157c7d..d6a766d383 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1668,7 +1668,7 @@ magic_methods = ( ) numerics = ( - "add sub mul div floordiv mod lshift rshift and xor or pow truediv" + "add sub mul matmul div floordiv mod lshift rshift and xor or pow truediv" ) inplace = ' '.join('i%s' % n for n in numerics.split()) right = ' '.join('r%s' % n for n in numerics.split()) diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py index cc0e707ece..f4a292afda 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -424,5 +424,16 @@ class TestMockingMagicMethods(unittest.TestCase): self.assertEqual(list(m), []) + def test_matmul(self): + m = MagicMock() + self.assertIsInstance(m @ 1, MagicMock) + m.__matmul__.return_value = 42 + m.__rmatmul__.return_value = 666 + m.__imatmul__.return_value = 24 + self.assertEqual(m @ 1, 42) + self.assertEqual(1 @ m, 666) + m @= 24 + self.assertEqual(m, 24) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS b/Misc/NEWS index 5c87046725..64d77aa59b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -18,6 +18,8 @@ Core and Builtins Library ------- +- Issue #23581: Add matmul support to MagicMock. Patch by HÃ¥kan Lövdahl. + - Issue #23566: enable(), register(), dump_traceback() and dump_traceback_later() functions of faulthandler now accept file descriptors. Patch by Wei Wu.