title: Using patch() to mock something that I don't explicilty import
body: <p>Let's say I have the following modules:</p>
  <pre class="lang-py prettyprint-override"><code># src/myapp/utils.py
  import thirdparty
  from myapp.secrets import get_super_secret_stuff
  
  def get_thirdparty_client() -&gt; return thirdparty.Client:
      thirdparty.Client(**get_super_secret_stuff())
  </code></pre>
  <pre class="lang-py prettyprint-override"><code># src/myapp/things.py
  from myapp.utils import get_thirdparty_client
  from myapp.transformations import apply_fairydust, make_it_sparkle
  
  def do_something(x):
      thirdparty_client = get_thirdparty_client()
      y = thidparty_client.query(apply_fairydust(x))
      return make_it_sparkle(y)
  </code></pre>
  <p>Assume that <code>myapp</code> is lightly-tested legacy code, and refactoring is out of the question. Also assume (annoyingly) that <code>thirdparty.Client</code> does non-deterministic network I/O in its <code>__init__</code> method. Therefore I intend to mock the <code>thirdparty.Client</code> class itself so as to make this <code>do_something</code> function testable.</p>
  <p>Assume also that I must use <code>unittest</code> and cannot use another test framework like Pytest.</p>
  <p>It seems like the <code>patch</code> function from <code>unittest.mock</code> is the right tool for the job. However, I'm unsure of how to apply the usual admonition to &quot;patch where it is used.&quot;</p>
  <p>Ideally I want to write a test that looks something like this:</p>
  <pre class="lang-py prettyprint-override"><code># tests/test_myapp/test_things.py
  from unittest import TestCase
  from unittest.mock import patch
  
  from myapp.things import do_something
  
  def gen_test_pairs():
      # Generate pairs of expected inputs and outputs
      ...
  
  class ThingTest(unittest.TestCase):
      @patch('????')
      def test_do_something(self, mock_thirdparty_client):
          for x, y in gen_test_pairs:
              with self.subTest(params={'x': x, 'y': y}):
                  mock_thirdparty_client.query.return_value = y
                  self.assertEqual(do_something(x), y)
  </code></pre>
  <p>My problem is that I don't know what to write in place of the <code>????</code>, because I never actually import <code>thirdparty.Client</code> in <code>src/myapp/things.py</code>.</p>
  <p>Options I considered:</p>
  <ol>
  <li>Apply the patch at <code>myapp.utils.thirdparty.Client</code>, which makes my test fragile and dependent on implementation details.</li>
  <li>&quot;Break the rules&quot; and apply the patch at <code>thirdparty.Client</code>.</li>
  <li>Import <code>get_thirdparty_client</code> in the test, use <code>patch.object</code> on it, and set its <code>return_value</code> to another <code>MagicMock</code> that I create separately, and this second mock would stand in for <code>thirdparty.Client</code>. This makes for more verbose testing code that can't easily be applied as a single decorator.</li>
  </ol>
  <p>None of these options sounds particularly appealing, but I don't know which is considered the least bad.</p>
  <p>Or is there another option available to me that I am not seeing?</p>