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.

Author dino.viehland
Recipients cjw296, dino.viehland, xtreak
Date 2020-02-04.23:10:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
My actual scenario involves a custom module loader where the modules are published are completely immutable (it ends up publishing an object which isn't a subtype of module).  It can still have normal Python modules as a child which aren't immutable, so they could still be patched by Mock (or it could have immutable sub-packages which Mock wouldn't be able to patch).  

So imagine something like this:

__immutable__ = True
x = 2

y = 2

Doing a "from immutable_package import x" would normally publish "x" as a child onto the package.  But because the package is immutable, this is impossible, and the assignment is ignored with a warning.  

When Mock gets a call to patch on something like "immutable_package.x.y", it's not going to find x, even though if I were to write "from immutable_package.x import y" or "from immutable_package import x" it would succeed.

Cases can be contrived without all of this though where the child isn't published on it's parent, but it requires 

from x.pkg import child

x = 1

from unittest.mock import patch
y = 42

@patch('x.pkg.child.y', 100)
def f():


"python -m x" will fail without the patch but succeed with it.
Date User Action Args
2020-02-04 23:10:23dino.viehlandsetrecipients: + dino.viehland, cjw296, xtreak
2020-02-04 23:10:23dino.viehlandsetmessageid: <>
2020-02-04 23:10:23dino.viehlandlinkissue39551 messages
2020-02-04 23:10:22dino.viehlandcreate