classification
Title: Mock autospec does not work with subclasses of property()
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: berker.peksag Nosy List: amaury.forgeotdarc, berker.peksag, gregory.p.smith, michael.foord, python-dev
Priority: normal Keywords: patch

Created on 2016-04-13 22:13 by amaury.forgeotdarc, last changed 2016-08-17 22:40 by gregory.p.smith. This issue is now closed.

Files
File name Uploaded Description Edit
mock-descriptor.patch amaury.forgeotdarc, 2016-04-13 22:13 review
mock-descriptor-2.patch amaury.forgeotdarc, 2016-05-18 11:47 review
Messages (7)
msg263361 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2016-04-13 22:13
When patching a class, mock.create_autospec() correctly detects properties and __slot__ attributes, but not subclasses of property() or other kinds of data descriptors.

The attached patch detects all data descriptors and patch them the way they should be.
msg265827 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2016-05-18 11:47
Updated patch with review comments.
msg272122 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-07 16:06
New changeset e947248100ae by Gregory P. Smith in branch '3.5':
Issue #26750: unittest.mock.create_autospec() now works properly
https://hg.python.org/cpython/rev/e947248100ae

New changeset 0bc14c91ef7e by Gregory P. Smith in branch 'default':
Issue #26750: unittest.mock.create_autospec() now works properly for
https://hg.python.org/cpython/rev/0bc14c91ef7e
msg272124 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-08-07 16:38
Thanks! Is there a reason not to use inspect.isdatadescriptor() instead of _is_data_descriptor()?
msg272215 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2016-08-09 04:40
Probably just Amaury and I forgetting that existed.  Amaury, inspect.isdatadescriptor's implementation is a bit different than this change's _is_data_descriptor.  Thoughts?
msg272842 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-16 06:56
New changeset 3ff02380b1bf by Gregory P. Smith in branch '3.5':
Issue #26750: use inspect.isdatadescriptor instead of our own
https://hg.python.org/cpython/rev/3ff02380b1bf

New changeset d51a66622266 by Gregory P. Smith in branch 'default':
Issue #26750: use inspect.isdatadescriptor instead of our own
https://hg.python.org/cpython/rev/d51a66622266
msg272997 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2016-08-17 22:37
inspect.isdatadescriptor() is better indeed.
(I was initially working on an old version of mock.py which does not import inspect, and I did not want to add the dependency there).

- inspect uses hasattr(type(obj)) instead of hasatr(obj). This is better, (but does not work for 2.x old-style classes)

- my patch tested for __del__... this is completely wrong, it should have been __delete__. oops.
inspect.isdatadescriptor() does not test for __delete__. This is insaccurate, but I doubt it will ever matter. This is only possible for Python-defined descriptors, the C implementation always exposes both __set__ and __delete__ when tp_set is filled.

IOW, I'm happy with the current state.
History
Date User Action Args
2016-08-17 22:40:09gregory.p.smithsetstage: commit review -> resolved
2016-08-17 22:37:19amaury.forgeotdarcsetmessages: + msg272997
2016-08-16 06:56:41python-devsetmessages: + msg272842
2016-08-09 04:40:38gregory.p.smithsetmessages: + msg272215
stage: resolved -> commit review
2016-08-07 16:38:35berker.peksagsetmessages: + msg272124
stage: commit review -> resolved
2016-08-07 16:07:15gregory.p.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> commit review
2016-08-07 16:06:41python-devsetnosy: + python-dev
messages: + msg272122
2016-08-07 14:43:22gregory.p.smithsetnosy: + gregory.p.smith
2016-05-18 11:49:07berker.peksagsetassignee: berker.peksag
2016-05-18 11:47:01amaury.forgeotdarcsetfiles: + mock-descriptor-2.patch

messages: + msg265827
2016-04-21 11:51:38berker.peksagsetversions: + Python 3.5, Python 3.6
nosy: + berker.peksag

components: + Library (Lib), - Tests
type: enhancement -> behavior
stage: patch review
2016-04-13 22:13:06amaury.forgeotdarccreate