title: Pytest monkeypatch not working on flask view function
body: <p>Suppose we have the following Flask view function to test. Specifically, we want to mock <code>create_foo()</code> as it writes to the filesystem.</p>
  
  <pre><code># proj_root/some_project/views.py
  from some_project import APP
  from some_project.libraries.foo import create_foo
  
  @APP.route('/run', methods=['POST']
  def run():
      bar = create_foo()
      return 'Running'
  </code></pre>
  
  <p>Now we want to write a unit test for <code>run()</code> with the call to <code>create_foo()</code> mocked to avoid creating unnecessary files.</p>
  
  <pre><code># proj_root/tests/test_views.py
  from some_project import some_project
  
  @pytest.fixture
  def client():
      some_project.APP.config['TESTING'] = True
      with some_project.APP.test_client() as client:
          yield client
  
  def test_run(client, monkeypatch):
      monkeypatch.setattr('some_project.libraries.foo.create_foo', lambda: None)
      response = client.post('/run')
      assert b'Running' in response.data
  </code></pre>
  
  <p>It seems that this approach should work even with the named <code>create_foo</code> import. The tests all pass, however the original code of <code>create_foo</code> is clearly being executed as a new file in the filesystem is created each time the test suite is run. What am I missing? I suspect it has something to do with the named imports based on some related questions but I'm not sure.</p>