title: How to mock nested function?
body: <p>I would like to mock some nested functions from a specific function:</p>
  
  <pre class="lang-py prettyprint-override"><code># tools.py
  def cpu_count():
      def get_cpu_quota():
          return int(load("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"))
  
      def get_cpu_period():
          return int(load("/sys/fs/cgroup/cpu/cpu.cfs_period_us"))
  
      def get_cpus():
          try:
              cfs_quota_us = get_cpu_quota()
              cfs_period_us = get_cpu_period()
              if cfs_quota_us &gt; 0 and cfs_period_us &gt; 0:
                  return int(math.ceil(cfs_quota_us / cfs_period_us))
          except:
              pass
          return multiprocessing.cpu_count()
  
      return get_cpus()
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code># test_tools.py
  class TestTools(unittest.TestCase):
  
      @patch("tools.cpu_count")
      def test_cpu_count_in_container(self, cpu_count_mock):
          cpu_count_mock.return_value.get_cpu_quota.return_value = 5000
          cpu_count_mock.return_value.get_cpu_period.return_value = 1000
  
          cpus = tools.cpu_count()
          self.assertEqual(5, cpus)
  </code></pre>
  
  <p>However, when running this test I have the follow error:</p>
  
  <pre><code>FAIL: test_cpu_count_in_container (tests.util.tools_test.ToolsTest)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/home/uilian/Development/foo/venv/lib/python3.7/site-packages/mock/mock.py", line 1305, in patched
      return func(*args, **keywargs)
    File "/home/uilian/Development/foo/tests/util/tools_test.py", line 154, in test_cpu_count_in_container
      self.assertIsInstance(cpus, int)
  AssertionError: &lt;MagicMock name='cpu_count()' id='140490818322384'&gt; is not an instance of &lt;class 'int'&gt;
  
  </code></pre>
  
  <p>Is it possible mocking nested functions?</p>
  
  <p>If not, what's the best approach for this case?</p>