Author dino.viehland
Recipients cjw296, dino.viehland, xtreak
Date 2020-02-06.21:27:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1581024470.63.0.9100873746.issue39551@roundup.psfhosted.org>
In-reply-to
Content
Sorry, publish may not necessarily be the best term.  When you do "from foo import bar" and foo is a package Python will set the bar module onto the foo module.  That leads to the common mistake of doing "import foo" and then "foo.bar.baz" and later removing the thing that do "from foo import bar" and having things blow up later.

Without additional support there's no way to patch the immutable module.  We can provide a mode where we enable the patching for testing and disable it in a production environment though.  That basically just involves passing a proxy object down to Mock.  And monkey patching the mutable module is perfectly fine.

The thing that's doing the ignoring of the assignment is the import system.  So it's now okay if the package raises an AttributeError.

There's not really a great way to work around this other than just bypassing mock's resolution of the object here - i.e. replacing mock.patch along with _get_target, _importer, and _dot_lookup and calling mock._patch directly, which isn't very nice (but is do-able).

And while this is a strange way to arrive at a module existing in sys.modules but not being on the package it is something that can happen in the normal course of imports, hence the reason why the import system handles this by checking sys.modules today.  It's also just turning what is currently an error while mocking into a success case with a simple few line change.
History
Date User Action Args
2020-02-06 21:27:50dino.viehlandsetrecipients: + dino.viehland, cjw296, xtreak
2020-02-06 21:27:50dino.viehlandsetmessageid: <1581024470.63.0.9100873746.issue39551@roundup.psfhosted.org>
2020-02-06 21:27:50dino.viehlandlinkissue39551 messages
2020-02-06 21:27:50dino.viehlandcreate