title: Python testing: mocking a function that's imported AND used inside another funciton
body: <p>Due to circular-import issues which are common with Celery tasks in Django, I'm often importing Celery tasks inside of my methods, like so:</p>
  
  <pre><code># some code omitted for brevity
  # accounts/models.py
  def refresh_library(self, queue_type="regular"):
      from core.tasks import refresh_user_library 
  
      refresh_user_library.apply_async(
          kwargs={"user_id": self.user.id}, queue=queue_type
      )
      return 0
  </code></pre>
  
  <p>In my <code>pytest</code> test for <code>refresh_library</code>, I'd only like to test that <code>refresh_user_library</code> (the Celery task) is called with the correct <code>args</code> and <code>kwargs</code>. But this isn't working:</p>
  
  <pre><code># tests/test_accounts_models.py
  @mock.patch("accounts.models.UserProfile.refresh_library.refresh_user_library")
  def test_refresh_library():
  </code></pre>
  
  <p>Error is about <code>refresh_library</code> not having an attribute <code>refresh_user_library</code>.</p>
  
  <p>I suspect this is due to the fact that the task(<code>refresh_user_library</code>) is imported inside the function itself, but I'm not too experienced with mocking so this might be completely wrong.</p>