title: Mock pathlib.Path.exists
body: <p>My Python function creates <code>pathlib.Path</code> objects. I want to mock this, so when the code will call <code>pathlib.Path(&quot;/tmp/a&quot;).exists()</code> it will get <code>True</code>, and when the code will call <code>pathlib.Path(&quot;/tmp/b&quot;).exists()</code> it will get <code>False</code>.</p>
  <p>I tried this:</p>
  <pre class="lang-py prettyprint-override"><code>import pathlib
  from unittest.mock import patch
  
  def my_side_effect(*args, **kwargs):
      print (f&quot; args = {args} , kwargs={kwargs}&quot;)
      return True
  
  with patch.object(pathlib.Path, 'exists') as mock_exists:
      mock_exists.side_effect = my_side_effect
      print(pathlib.Path(&quot;a&quot;).exists())
  </code></pre>
  <p>Output:</p>
  <pre><code> args = () , kwargs={}
  True
  </code></pre>
  <p>As you can see, my side effect is not getting any args/kwargs based on the path.</p>