OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python mock patch call_args_list type

  • Thread starter Thread starter eddie
  • Start date Start date
E

eddie

Guest
Looking into path call_args_list feature, and I found something strange. Running the bellow code on Python3.7 and python3.11 gives different types.On Python3.7 the type is <class 'unittest.mock._Call'> while on python3.11 the type is <class 'dict'>.

  1. Can't find it in the official documentation, where it was changed?
  2. What's the proper way to work with the call args? Let's say I want to validate that the kwarg named foo was set to "bar" in the call to the mocked function (with dict is very easy, with _Call I haven't found a way to do so).

Code:
from unittest import mock

def foobar(foo = None, bar = None):
 print(f'args: {foo}, {bar}')

l = None
with mock.patch('__main__.foobar') as m:
 foobar(foo=1, bar=4)
 l = m.call_args_list

type(l[0].kwargs)
<p>Looking into path call_args_list feature, and I found something strange.
Running the bellow code on Python3.7 and python3.11 gives different types.On Python3.7 the type is <class 'unittest.mock._Call'> while on python3.11 the type is <class 'dict'>.<br/></p>
<ol>
<li>Can't find it in the official documentation, where it was changed?<br/></li>
<li>What's the proper way to work with the call args? Let's say I want to validate that the kwarg named foo was set to "bar" in the call to the mocked function (with dict is very easy, with _Call I haven't found a way to do so).</li>
</ol>
<pre class="lang-py prettyprint-override"><code>from unittest import mock

def foobar(foo = None, bar = None):
print(f'args: {foo}, {bar}')

l = None
with mock.patch('__main__.foobar') as m:
foobar(foo=1, bar=4)
l = m.call_args_list

type(l[0].kwargs)
</code></pre>
 

Latest posts

Top