From 6c4fab0f4b95410a1a964a75dcdd953697eff089 Mon Sep 17 00:00:00 2001 From: John Reese Date: Tue, 22 May 2018 13:01:10 -0700 Subject: [PATCH] bpo-33516: Add support for __round__ in MagicMock (GH-6880) unittest.mock.MagicMock now supports the __round__() magic method. --- Doc/library/unittest.mock.rst | 2 +- Lib/unittest/mock.py | 2 +- Lib/unittest/test/testmock/testmagicmethods.py | 5 +++++ .../next/Library/2018-05-15-17-06-42.bpo-33516.ZzARe4.rst | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-05-15-17-06-42.bpo-33516.ZzARe4.rst diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index bb647bb6a8..7dad3340ad 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -1660,7 +1660,7 @@ The full list of supported magic methods is: * ``__hash__``, ``__sizeof__``, ``__repr__`` and ``__str__`` * ``__dir__``, ``__format__`` and ``__subclasses__`` -* ``__floor__``, ``__trunc__`` and ``__ceil__`` +* ``__round__``, ``__floor__``, ``__trunc__`` and ``__ceil__`` * Comparisons: ``__lt__``, ``__gt__``, ``__le__``, ``__ge__``, ``__eq__`` and ``__ne__`` * Container methods: ``__getitem__``, ``__setitem__``, ``__delitem__``, diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 9302dedae7..e9bb463590 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1709,7 +1709,7 @@ magic_methods = ( # because there is no idivmod "divmod rdivmod neg pos abs invert " "complex int float index " - "trunc floor ceil " + "round trunc floor ceil " "bool next " ) diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py index 37623dcebc..5ab95978f6 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -1,3 +1,4 @@ +import math import unittest import sys from unittest.mock import Mock, MagicMock, _magics @@ -280,6 +281,10 @@ class TestMockingMagicMethods(unittest.TestCase): self.assertEqual(hash(mock), object.__hash__(mock)) self.assertEqual(str(mock), object.__str__(mock)) self.assertTrue(bool(mock)) + self.assertEqual(round(mock), mock.__round__()) + self.assertEqual(math.trunc(mock), mock.__trunc__()) + self.assertEqual(math.floor(mock), mock.__floor__()) + self.assertEqual(math.ceil(mock), mock.__ceil__()) # in Python 3 oct and hex use __index__ # so these tests are for __index__ in py3k diff --git a/Misc/NEWS.d/next/Library/2018-05-15-17-06-42.bpo-33516.ZzARe4.rst b/Misc/NEWS.d/next/Library/2018-05-15-17-06-42.bpo-33516.ZzARe4.rst new file mode 100644 index 0000000000..77b1428f3c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-05-15-17-06-42.bpo-33516.ZzARe4.rst @@ -0,0 +1 @@ +:class:`unittest.mock.MagicMock` now supports the ``__round__`` magic method. -- 2.40.0