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.

classification
Title: Add mock_import method to mock module
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: Eyal Posener, fruch, iritkatriel, michael.foord
Priority: normal Keywords: patch

Created on 2016-06-23 17:12 by Eyal Posener, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mock_import.patch Eyal Posener, 2016-06-23 17:12 a patch with the new method review
Messages (7)
msg269127 - (view) Author: Eyal Posener (Eyal Posener) * Date: 2016-06-23 17:12
Add mock_import method.

A helper function to mask ``ImportError``s on a scoped code, using the ``with`` statement, or in method a method used as a decorator.
Failed imports will be ignored, unless specified by the *do_not_mock* argument.

The *do_not_mock* argument is a package or module name, or package or module names list. When specified, and imported in the scoped mocked code, importing them must succeed. If ``None`` (the default) then no import must succeed.
msg274103 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2016-09-01 08:51
Is this for mocking out runtime dependencies that aren't available at test time? It seems like a good way of masking bugs! I'd be happier with a (or at least an option) to specify the imports that should be mocked. The use case should be mentioned in the docs.

I think the name is slightly confusing. I originally thought this was a function to mock specific imports - not to catch failed imports. mock_missing_import (or similar) would be a better name.

It's common with the mock functions to be able to provide a class to use as the mock function, and to take arbitrary keyword arguments to pass to the mock constructor.
msg274104 - (view) Author: Eyal Posener (Eyal Posener) * Date: 2016-09-01 09:02
Thanks for the review, Michael.

About the use case: I use it for a process with loads code and inspect it's classes and methods. When I run this process, not always I have all the dependencies of the inspected code, so I found myself mocking all those packages before running the inspection code. This was very inconvenience, and broke any time someone added a new dependency to the code which is not in the standard library.

About the name: I agree.

About the keyword for the mock constructor: no problems.

Should I fix the code and submit an updated patch?
Do you think this function has a place in the standard mock module?
msg274106 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2016-09-01 09:08
It's not a use case I've specifically had but I can see its use. I'm uncertain of whether that means it belongs in the module or not. Let me see if I can get some more eyes on this bug.
msg274108 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2016-09-01 09:19
Before you spend any more time on this, my current thinking is that this is a bit too specialised to belong in the standard library. I'll wait and see if a preponderance of core devs and other users disagree with me before I close this though.
msg274153 - (view) Author: Israel Fruchter (fruch) Date: 2016-09-01 16:55
Some real use cases is needed, like testing a code that behave differently on case of package availability.

I think something like patch for modules can be useful here, so you could have some control on what would be returned.
msg416574 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-04-02 18:20
Closing as that's where the discussion of 2016 was leaning. Please raise this on python-ideas if you would like to bring it up again.
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71563
2022-04-02 18:20:53iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg416574

resolution: rejected
stage: patch review -> resolved
2016-09-01 16:55:45fruchsetnosy: + fruch
messages: + msg274153
2016-09-01 09:19:10michael.foordsetmessages: + msg274108
2016-09-01 09:08:34michael.foordsetmessages: + msg274106
2016-09-01 09:02:07Eyal Posenersetmessages: + msg274104
2016-09-01 08:51:43michael.foordsetmessages: + msg274103
2016-09-01 08:47:14michael.foordsetassignee: michael.foord
2016-06-23 20:14:31SilentGhostsetnosy: + michael.foord

stage: patch review
2016-06-23 17:12:17Eyal Posenercreate