Dino Viehland <dinoviehl...@gmail.com> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39551>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to