summaryrefslogtreecommitdiff
path: root/Lib/unittest
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
commit8833eb1c084c11fd8889cdb01d9cff76c2daa838 (patch)
tree0c57a2ad166c93b3965eb7484d00a39d5308c8f4 /Lib/unittest
parent2c3eb5cfe907517f1d4fa45ebc173fb6394a6aed (diff)
downloadcpython-8833eb1c084c11fd8889cdb01d9cff76c2daa838.tar.gz
Issue #26750: unittest.mock.create_autospec() now works properly
for subclasses of property() and other data descriptors.
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py10
-rw-r--r--Lib/unittest/test/testmock/testhelpers.py60
2 files changed, 48 insertions, 22 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 = {}
diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py
index 1dbc0b64ba..34776347da 100644
--- a/Lib/unittest/test/testmock/testhelpers.py
+++ b/Lib/unittest/test/testmock/testhelpers.py
@@ -802,35 +802,53 @@ class SpecSignatureTest(unittest.TestCase):
a.f.assert_called_with(self=10)
- def test_autospec_property(self):
- class Foo(object):
- @property
- def foo(self):
- return 3
+ def test_autospec_data_descriptor(self):
+ class Descriptor(object):
+ def __init__(self, value):
+ self.value = value
- foo = create_autospec(Foo)
- mock_property = foo.foo
+ def __get__(self, obj, cls=None):
+ if obj is None:
+ return self
+ return self.value
- # no spec on properties
- self.assertIsInstance(mock_property, MagicMock)
- mock_property(1, 2, 3)
- mock_property.abc(4, 5, 6)
- mock_property.assert_called_once_with(1, 2, 3)
- mock_property.abc.assert_called_once_with(4, 5, 6)
+ def __set__(self, obj, value):
+ pass
+ class MyProperty(property):
+ pass
- def test_autospec_slots(self):
class Foo(object):
- __slots__ = ['a']
+ __slots__ = ['slot']
+
+ @property
+ def prop(self):
+ return 3
+
+ @MyProperty
+ def subprop(self):
+ return 4
+
+ desc = Descriptor(42)
foo = create_autospec(Foo)
- mock_slot = foo.a
- # no spec on slots
- mock_slot(1, 2, 3)
- mock_slot.abc(4, 5, 6)
- mock_slot.assert_called_once_with(1, 2, 3)
- mock_slot.abc.assert_called_once_with(4, 5, 6)
+ def check_data_descriptor(mock_attr):
+ # Data descriptors don't have a spec.
+ self.assertIsInstance(mock_attr, MagicMock)
+ mock_attr(1, 2, 3)
+ mock_attr.abc(4, 5, 6)
+ mock_attr.assert_called_once_with(1, 2, 3)
+ mock_attr.abc.assert_called_once_with(4, 5, 6)
+
+ # property
+ check_data_descriptor(foo.prop)
+ # property subclass
+ check_data_descriptor(foo.subprop)
+ # class __slot__
+ check_data_descriptor(foo.slot)
+ # plain data descriptor
+ check_data_descriptor(foo.desc)
class TestCallList(unittest.TestCase):