Message361377
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. |
|
Date |
User |
Action |
Args |
2020-02-04 23:10:23 | dino.viehland | set | recipients:
+ dino.viehland, cjw296, xtreak |
2020-02-04 23:10:23 | dino.viehland | set | messageid: <1580857823.02.0.691386806641.issue39551@roundup.psfhosted.org> |
2020-02-04 23:10:23 | dino.viehland | link | issue39551 messages |
2020-02-04 23:10:22 | dino.viehland | create | |
|