Skip to content

Commit a327677

Browse files
authored
bpo-39485: fix corner-case in method-detection of mock (GH-18252)
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.
1 parent 3cb49b6 commit a327677

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
@@ -2748,7 +2748,7 @@ def _must_skip(spec, entry, is_type):
27482748
continue
27492749
if isinstance(result, (staticmethod, classmethod)):
27502750
return False
2751-
elif isinstance(getattr(result, '__get__', None), MethodWrapperTypes):
2751+
elif isinstance(result, FunctionTypes):
27522752
# Normal method => skip if looked up on type
27532753
# (if looked up on instance, self is already skipped)
27542754
return is_type
@@ -2778,10 +2778,6 @@ def __init__(self, spec, spec_set=False, parent=None,
27782778
type(ANY.__eq__),
27792779
)
27802780

2781-
MethodWrapperTypes = (
2782-
type(ANY.__eq__.__get__),
2783-
)
2784-
27852781

27862782
file_spec = None
27872783

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)