New submission from john passaro <john.a.pass...@gmail.com>:

I expected the following code to print True:

<pre>
import os
from unittest.mock import call, Mock, patch

parent = Mock()
parent.getenv = patch('os.getenv')
with parent.getenv:
    os.getenv('FOO', 'bar')
expected = [call.getenv('FOO', 'bar')]
print(expected, '==', parent.mock_calls, ':', expected == parent.mock_calls)
</pre>

It works fine if you replace the statement `parent.getenv = patch('os.getenv')` 
with:
<pre>
parent.getenv = patch('os.getenv', _mock_parent=parent,
                      _mock_new_parent=parent, _mock_new_name='getenv')
</pre>


Background:

I was trying to make assertions about a mocked call within a mocked context 
generator.

<pre>
# c.py
from . import a
from . import b
def func():
    with a.context('c'):
        b.operation()
</pre>
<pre>
# test_c.py
from package.c import func
from unittest.mock import Mock, call, patch
parent = Mock()
parent.context = patch('package.a.context')
parent.operation = patch('package.b.operation')
with parent.context, parent.operation:
    func()
assert parent.mock_calls == [
   call.context('c'),
   call.context().__enter__(),
   call.operation(),
   call.context().__exit__(None, None, None),
]
</pre>

in other words, to ensure the correct order of the __enter__/__exit__ calls 
relative to the actual operation. I have my workaround but it's very awkward 
looking.

----------
components: Library (Lib)
messages: 373654
nosy: thinkingmachin6
priority: normal
severity: normal
status: open
title: unittest.mock: patched mocks do not propagate calls to parent when set 
via setattr
type: behavior
versions: Python 3.8

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

Reply via email to