[Python-checkins] bpo-39485: fix corner-case in method-detection of mock (GH-18256)

Miss Islington (bot) webhook-mailer at python.org
Wed Jan 29 11:10:32 EST 2020


https://github.com/python/cpython/commit/cf0645a17acbc0c4dbbf82434e37637965748bbb
commit: cf0645a17acbc0c4dbbf82434e37637965748bbb
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-01-29T16:10:24Z
summary:

bpo-39485: fix corner-case in method-detection of mock (GH-18256)

Replace check for whether something is a method in the mock module. The
previous version fails on PyPy, because there no method wrappers exist
(everything looks like a regular Python-defined function). Thus the
isinstance(getattr(result, '__get__', None), MethodWrapperTypes) check
returns True for any descriptor, not just methods.

This condition could also return erroneously True in CPython for
C-defined descriptors.

Instead to decide whether something is a method, just check directly
whether it's a function defined on the class. This passes all tests on
CPython and fixes the bug on PyPy.
(cherry picked from commit a327677905956ae0b239ff430a1346dfe265709e)

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>

files:
A Misc/NEWS.d/next/Library/2020-01-29-14-58-27.bpo-39485.Zy3ot6.rst
M Lib/unittest/mock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 372a6719be9fa..87e8735e958d5 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2342,7 +2342,7 @@ def _must_skip(spec, entry, is_type):
             continue
         if isinstance(result, (staticmethod, classmethod)):
             return False
-        elif isinstance(getattr(result, '__get__', None), MethodWrapperTypes):
+        elif isinstance(result, FunctionTypes):
             # Normal method => skip if looked up on type
             # (if looked up on instance, self is already skipped)
             return is_type
@@ -2381,10 +2381,6 @@ def __init__(self, spec, spec_set=False, parent=None,
     type(ANY.__eq__),
 )
 
-MethodWrapperTypes = (
-    type(ANY.__eq__.__get__),
-)
-
 
 file_spec = None
 
diff --git a/Misc/NEWS.d/next/Library/2020-01-29-14-58-27.bpo-39485.Zy3ot6.rst b/Misc/NEWS.d/next/Library/2020-01-29-14-58-27.bpo-39485.Zy3ot6.rst
new file mode 100644
index 0000000000000..f62c31fc686ad
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-29-14-58-27.bpo-39485.Zy3ot6.rst
@@ -0,0 +1,3 @@
+Fix a bug in :func:`unittest.mock.create_autospec` that would complain about
+the wrong number of arguments for custom descriptors defined in an extension
+module returning functions.



More information about the Python-checkins mailing list