classification
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
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Aaron Gallagher, berker.peksag, michael.foord
Priority: normal Keywords: patch

Created on 2017-01-31 23:01 by Aaron Gallagher, last changed 2017-02-22 04:58 by Aaron Gallagher.

Files
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 (2)
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 https://github.com/testing-cabal/mock/pull/389
History
Date User Action Args
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