title: How to patch 'open' from an imported module using Mock
body: <p>I am writing some unit tests for a module I have. I need to patch <code>open</code> so when the functions inside the tested module calls <code>open</code> the mock is used instead of the real <code>open</code>.</p>
  
  <p>This code works but I think it would break another tests because is not restoring <code>open</code> to its original value:</p>
  
  <pre><code>class TestCases(unittest.TestCase):
      def test_something(self):
          from amodule import bmodule
  
          open_mock = mock.MagicMock(spec=open)
          bmodule.__builtins__['open'] = open_mock
          read_mock = mock.MagicMock()
          open_mock.return_value.__enter__.return_value = read_mock
  
          self.assertTrue(bmodule.some_function())
          self.assertEqual(open_mock.call_args_list, ['filename1', 'filename2'])
  </code></pre>
  
  <p>How can I do this using <code>mock.patch</code>?</p>