[Python-checkins] bpo-35022: unittest.mock.MagicMock now also supports __fspath__ (GH-9960)

Victor Stinner webhook-mailer at python.org
Thu Oct 25 17:49:02 EDT 2018


https://github.com/python/cpython/commit/6c83d9f4a72905d968418bef670bb3091d2744db
commit: 6c83d9f4a72905d968418bef670bb3091d2744db
branch: master
author: Max Bélanger <aeromax at gmail.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2018-10-25T23:48:58+02:00
summary:

bpo-35022: unittest.mock.MagicMock now also supports __fspath__ (GH-9960)

The MagicMock class supports many magic methods, but not __fspath__. To ease
testing with modules such as os.path, this function is now supported by default.

files:
A Misc/NEWS.d/next/Library/2018-10-18-17-57-28.bpo-35022.KeEF4T.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 136804cfc2c8..0ae29546586a 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -1699,6 +1699,10 @@ The full list of supported magic methods is:
 * Descriptor methods: ``__get__``, ``__set__`` and ``__delete__``
 * Pickling: ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``,
   ``__getnewargs__``, ``__getstate__`` and ``__setstate__``
+* File system path representation: ``__fspath__``
+
+.. versionchanged:: 3.8
+   Added support for :func:`os.PathLike.__fspath__`.
 
 
 The following methods exist but are *not* supported as they are either in use
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 6b7f293bc5e0..1a6c1a606c5a 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1713,6 +1713,7 @@ def _patch_stopall():
     "complex int float index "
     "round trunc floor ceil "
     "bool next "
+    "fspath "
 )
 
 numerics = (
@@ -1760,6 +1761,7 @@ def method(self, *args, **kw):
     '__hash__': lambda self: object.__hash__(self),
     '__str__': lambda self: object.__str__(self),
     '__sizeof__': lambda self: object.__sizeof__(self),
+    '__fspath__': lambda self: f"{type(self).__name__}/{self._extract_mock_name()}/{id(self)}",
 }
 
 _return_values = {
diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py
index 5ab95978f60d..69dfe60f7eae 100644
--- a/Lib/unittest/test/testmock/testmagicmethods.py
+++ b/Lib/unittest/test/testmock/testmagicmethods.py
@@ -1,5 +1,6 @@
 import math
 import unittest
+import os
 import sys
 from unittest.mock import Mock, MagicMock, _magics
 
@@ -293,6 +294,15 @@ def test_magicmock_defaults(self):
         # how to test __sizeof__ ?
 
 
+    def test_magic_methods_fspath(self):
+        mock = MagicMock()
+        expected_path = mock.__fspath__()
+        mock.reset_mock()
+
+        self.assertEqual(os.fspath(mock), expected_path)
+        mock.__fspath__.assert_called_once()
+
+
     def test_magic_methods_and_spec(self):
         class Iterable(object):
             def __iter__(self):
diff --git a/Misc/NEWS.d/next/Library/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst b/Misc/NEWS.d/next/Library/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst
new file mode 100644
index 000000000000..426be70ceacf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst
@@ -0,0 +1,2 @@
+:class:`unittest.mock.MagicMock` now supports the ``__fspath__`` method
+(from :class:`os.PathLike`).



More information about the Python-checkins mailing list