This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Show how to use mock_open in modules other that __main__
Type: enhancement Stage: patch review
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: berker.peksag, butla, docs@python, michael.foord
Priority: normal Keywords: patch

Created on 2016-11-18 13:17 by butla, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
mock_open_doc.patch butla, 2016-11-18 13:17 review
Messages (6)
msg281114 - (view) Author: Michał Bultrowicz (butla) * Date: 2016-11-18 13:17
Documentation of mock_open doesn't say how to use it in real-life test situations (when you're probably not mocking in __main__). I've spent some time scratching my head and googling for the way to mock-out the "open" function, want to spare other people the hassle.

The thing is that "open" needs to be mocked out from the magical "builtins" module, and not from the place of usage (like when mocking everything that's not built-in). So it's not obvious how to do that, especially that the example with __main__ makes it look like the normal mocking approach should work.

I still don't fully understand why mocking "" can work from interpreter, but that's a different thing...
msg281731 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-11-25 17:05
Thanks for the report and for the patch. I think expanding the "Where to patch" [1] section would be better. We can then refer to it from the mock_open() documentation.

msg281975 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2016-11-29 09:12
open shouldn't always be patched in builtins, it's much better to patch it in the specific namespace it's being called from. So the doc patch here shouldn't be applied as is.
msg281991 - (view) Author: Michał Bultrowicz (butla) * Date: 2016-11-29 14:44
Then where it should be patched in? Can you give an example? From what I've checked patching only works in __main__ and builtins.
msg282096 - (view) Author: Michał Bultrowicz (butla) * Date: 2016-11-30 19:37
Ok, I've checked again and now patching "" works. I have no idea what I was doing wrong the last time I checked... So I guess I'll close the issue.
msg282176 - (view) Author: Michał Bultrowicz (butla) * Date: 2016-12-01 13:31
One more update - I had the problem, because I was using monkeypatch.setattr() from Pytest, and assumed that it will work the same as patch(). This assumption turned out to be wrong.
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72919
2016-12-01 13:31:53butlasetmessages: + msg282176
2016-11-30 19:37:19butlasetstatus: open -> closed
2016-11-30 19:37:04butlasetmessages: + msg282096
2016-11-29 14:44:20butlasetmessages: + msg281991
2016-11-29 09:12:40michael.foordsetmessages: + msg281975
2016-11-25 17:05:50berker.peksagsetversions: - Python 3.3, Python 3.4
nosy: + berker.peksag, michael.foord

messages: + msg281731

stage: patch review
2016-11-18 13:17:52butlacreate