[Python-checkins] bpo-33516: Add support for __round__ in MagicMock (GH-6880)

Victor Stinner webhook-mailer at python.org
Tue May 22 16:01:16 EDT 2018


https://github.com/python/cpython/commit/6c4fab0f4b95410a1a964a75dcdd953697eff089
commit: 6c4fab0f4b95410a1a964a75dcdd953697eff089
branch: master
author: John Reese <john at noswap.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2018-05-22T22:01:10+02:00
summary:

bpo-33516: Add support for __round__ in MagicMock (GH-6880)

unittest.mock.MagicMock now supports the __round__() magic method.

files:
A Misc/NEWS.d/next/Library/2018-05-15-17-06-42.bpo-33516.ZzARe4.rst
M Doc/library/unittest.mock.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmagicmethods.py

diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index bb647bb6a811..7dad3340ad61 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 9302dedae7fd..e9bb46359060 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1709,7 +1709,7 @@ def _patch_stopall():
     # 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 37623dcebc6c..5ab95978f60d 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 @@ def test_magicmock_defaults(self):
         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 000000000000..77b1428f3c87
--- /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.



More information about the Python-checkins mailing list