title: Python: How to mock a value at import time?
body: <p>I would like to test a class (A) that extends another class (B). For my test I would like to mock class B to modify the behaviour of A but in order to do that I need to be able to apply the mock at import time. How would I do that? Please see below.</p>
  <p><strong>database.py</strong></p>
  <pre><code>from flask_sqlalchemy import SQLAlchemy
  db = SQLAlchemy()
  </code></pre>
  <p><strong>models.py</strong></p>
  <pre><code>from .database import db
  
  class Meta(db.Model):
      ...
  </code></pre>
  <p>In my unit test, I would like Meta to extend the standard python declarative Base so essentially I would like to mock database.py to instead act as if I'd done something like this:</p>
  <pre><code>db.Model = declarative_base()
  </code></pre>
  <p>My test currently looks like
  <strong>test_model.py</strong></p>
  <pre><code>from pkg.models import Meta
  
  test_model()
      assert(...)
  </code></pre>
  <p>Thanks for the help.</p>