Skip to content

Commit cf0645a

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

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
@@ -2342,7 +2342,7 @@ def _must_skip(spec, entry, is_type):
23422342
continue
23432343
if isinstance(result, (staticmethod, classmethod)):
23442344
return False
2345-
elif isinstance(getattr(result, '__get__', None), MethodWrapperTypes):
2345+
elif isinstance(result, FunctionTypes):
23462346
# Normal method => skip if looked up on type
23472347
# (if looked up on instance, self is already skipped)
23482348
return is_type
@@ -2381,10 +2381,6 @@ def __init__(self, spec, spec_set=False, parent=None,
23812381
type(ANY.__eq__),
23822382
)
23832383

2384-
MethodWrapperTypes = (
2385-
type(ANY.__eq__.__get__),
2386-
)
2387-
23882384

23892385
file_spec = None
23902386

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)