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.
This commit is contained in:
parent
3cb49b62e6
commit
a327677905
|
@ -2748,7 +2748,7 @@ def _must_skip(spec, entry, is_type):
|
||||||
continue
|
continue
|
||||||
if isinstance(result, (staticmethod, classmethod)):
|
if isinstance(result, (staticmethod, classmethod)):
|
||||||
return False
|
return False
|
||||||
elif isinstance(getattr(result, '__get__', None), MethodWrapperTypes):
|
elif isinstance(result, FunctionTypes):
|
||||||
# Normal method => skip if looked up on type
|
# Normal method => skip if looked up on type
|
||||||
# (if looked up on instance, self is already skipped)
|
# (if looked up on instance, self is already skipped)
|
||||||
return is_type
|
return is_type
|
||||||
|
@ -2778,10 +2778,6 @@ FunctionTypes = (
|
||||||
type(ANY.__eq__),
|
type(ANY.__eq__),
|
||||||
)
|
)
|
||||||
|
|
||||||
MethodWrapperTypes = (
|
|
||||||
type(ANY.__eq__.__get__),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
file_spec = None
|
file_spec = None
|
||||||
|
|
||||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue