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 <1580857823.02.0.691386806641.issue39551@roundup.psfhosted.org>
In-reply-to
Content
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_package\__init__.py
__immutable__ = True
x = 2

immutable_package\x.py
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 

x/__init__.py
from x.pkg import child

x/pkg/__init__.py:
x = 1


x/pkg/child.py:
from unittest.mock import patch
y = 42

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

f()

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