Title: mock's autospec's behavior on method-bound builtin functions is broken
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Aaron Gallagher, berker.peksag, csabella, michael.foord
Priority: normal Keywords: patch

Created on 2017-01-31 23:01 by Aaron Gallagher, last changed 2017-06-06 11:16 by csabella.

File name Uploaded Description Edit
mock.patch Aaron Gallagher, 2017-01-31 23:08 review
Pull Requests
URL Status Linked Edit
PR 3 Aaron Gallagher, 2017-02-22 04:58
Messages (3)
msg286565 - (view) Author: Aaron Gallagher (Aaron Gallagher) * Date: 2017-01-31 23:01
Cython will, in the right circumstances, offer a MethodType instance where im_func is a builtin function. Any instance of MethodType is automatically assumed to be a python-defined function (more specifically, a function that has an inspectable signature), but _set_signature was still conservative in its assumptions. As a result _set_signature would return early with None instead of a mock since the im_func had no inspectable signature. This causes problems deeper inside mock, as _set_signature is assumed to always return a mock, and nothing checked its return value.

In similar corner cases, autospec will simply not check the spec of the function, so _set_signature is amended to now return early with the original, not-wrapped mock object.

There is another None early return in _set_signature, but it seems.. harder to trigger? It seems more appropriate to raise an exception there if this is being fixed.
msg286566 - (view) Author: Aaron Gallagher (Aaron Gallagher) * Date: 2017-01-31 23:08
Patch adapted from
msg295249 - (view) Author: Cheryl Sabella (csabella) * Date: 2017-06-06 11:16
This PR appears to have been approved, perhaps pending input from michael.foord?
Date User Action Args
2017-06-06 11:16:56csabellasetnosy: + csabella
messages: + msg295249
2017-02-22 04:58:30Aaron Gallaghersetpull_requests: + pull_request193
2017-02-13 20:54:25berker.peksagsetnosy: + michael.foord, berker.peksag
stage: patch review

versions: - Python 2.7, Python 3.3, Python 3.4
2017-01-31 23:08:03Aaron Gallaghersetfiles: + mock.patch
keywords: + patch
messages: + msg286566
2017-01-31 23:01:55Aaron Gallaghercreate