Skip to content

Commit 696d232

Browse files
bpo-39485: fix corner-case in method-detection of mock (GH-18255)
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 a327677) Co-authored-by: Carl Friedrich Bolz-Tereick <[email protected]> Co-authored-by: Carl Friedrich Bolz-Tereick <[email protected]>
1 parent 2b675f0 commit 696d232

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,7 @@ def _must_skip(spec, entry, is_type):
27152715
continue
27162716
if isinstance(result, (staticmethod, classmethod)):
27172717
return False
2718-
elif isinstance(getattr(result, '__get__', None), MethodWrapperTypes):
2718+
elif isinstance(result, FunctionTypes):
27192719
# Normal method => skip if looked up on type
27202720
# (if looked up on instance, self is already skipped)
27212721
return is_type
@@ -2745,10 +2745,6 @@ def __init__(self, spec, spec_set=False, parent=None,
27452745
type(ANY.__eq__),
27462746
)
27472747

2748-
MethodWrapperTypes = (
2749-
type(ANY.__eq__.__get__),
2750-
)
2751-
27522748

27532749
file_spec = None
27542750

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug in :func:`unittest.mock.create_autospec` that would complain about
2+
the wrong number of arguments for custom descriptors defined in an extension
3+
module returning functions.

0 commit comments

Comments
 (0)