summaryrefslogtreecommitdiff
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2016-08-07 08:52:26 -0700
committerGregory P. Smith <greg@krypto.org>2016-08-07 08:52:26 -0700
commit9854789efec0c707fff871b32b2833f32b078fb3 (patch)
tree7f659a7f58967939c1347560716b722d01493035 /Lib/unittest/mock.py
parentabfe28b01282d5f7c79b2e3cfb33ea29f9f3656f (diff)
downloadcpython-git-9854789efec0c707fff871b32b2833f32b078fb3.tar.gz
Issue #26750: unittest.mock.create_autospec() now works properly
for subclasses of property() and other data descriptors.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 7f30b287ae..83e9c46676 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -64,12 +64,20 @@ class _slotted(object):
__slots__ = ['a']
+# Do not use this tuple. It was never documented as a public API.
+# It will be removed. It has no obvious signs of users on github.
DescriptorTypes = (
type(_slotted.a),
property,
)
+def _is_data_descriptor(obj):
+ # Data descriptors are Properties, slots, getsets and C data members.
+ return ((hasattr(obj, '__set__') or hasattr(obj, '__del__')) and
+ hasattr(obj, '__get__'))
+
+
def _get_signature_object(func, as_instance, eat_self):
"""
Given an arbitrary, possibly callable object, try to create a suitable
@@ -2130,7 +2138,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
_kwargs.update(kwargs)
Klass = MagicMock
- if type(spec) in DescriptorTypes:
+ if _is_data_descriptor(spec):
# descriptors don't have a spec
# because we don't know what type they return
_kwargs = {}