title: SQLAlchemy object throwing attribute error for _sa_instance_state in pytest
body: <p>I can't find any other threads that discuss getting an attribute error for 'sa_instance_state' for a SQLAlchemy object. I'm getting this error when calling <code>Voterlist.query.all()</code> in a pytest method. I don't get this error when I call <code>Voterlist.query.all()</code> within the API or anywhere else.</p>
  <p>This is a Flask app with blueprints for the frontend and api. The frontend calls the API.</p>
  <p>conftest.py:</p>
  <pre><code>class TestConfig(Config):                                                                                                                                   
      TESTING = True                                                                                                                                          
      SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'                                                                                                           
      WTF_CSRF_ENABLED = False                                                                                                                                
      BCRYPT_LOG_ROUNDS = 4                                                                                                                                   
                                                                                                                                                              
  def fill_database():                                                                                                                                        
      s0 = Source(title='Catalist')                                                                                                                           
      s1 = Source(title='L2')                                                                                                                                 
      sources = [s0, s1]                                                                                                                                      
      l0 = Voterlist(description='Test List 0')                                                                                                               
      l1 = Voterlist(description='Test List 1')                                                                                                               
      lists = [l0, l1]                                                                                                                                        
      return lists, sources                                                                                                                                   
                                                                                                                                                              
  @pytest.fixture(scope='class')                                                                                                                              
  def test_client():                                                                                                                                          
      flask_app = create_app(TestConfig)                                                                                                                      
      with flask_app.test_client() as client:                                                                                                                 
          app_context = flask_app.test_request_context()                                                                                                      
          app_context.push()                                                                                                                                  
          db.create_all()                                                                                                                                     
          lists, sources = fill_database()                                                                                                                    
          for s in sources:                                                                                                                                   
              db.session.add(s)                                                                                                                               
              db.session.commit()                                                                                                                             
          for l in lists:                                                                                                                                     
              db.session.add(l)                                                                                                                        
              db.session.commit()                                                                                                                             
              sleep(1)                                                                                                                                        
          yield client                                                                                                                                        
          db.drop_all()                                                                                                                                       
      app_context.pop() 
  </code></pre>
  <p>test_api.py:</p>
  <pre><code>@pytest.mark.usefixtures('test_client')                                                                                                                     
  class TestLists():                                                                                                                                          
      def test_get_lists(self, test_client):                                                                                                                  
          '''Test download all lists.'''                                                                                                                      
          response = test_client.get(url_for('api.lists'))                                                                                                    
          resplists = response.json['lists']                                                                                                                  
          assert type(resplists) == list                                                                                                                      
          dblists = Voterlist.query.all() &lt;&lt;&lt;&lt; Error here
  </code></pre>
  <p>API method:</p>
  <pre><code>class ListAPI(Resource):                                                                                                                                    
      def __init__(self):                                                                                                                                     
          self.reqparse = reqparse.RequestParser()                                                                                                            
          self.fields = ['description', 'vendorquery', 'campaign', 's3key',                                                                                   
                         'ftimestamp', 'fname', 'source', 'user']                                                                                             
          for f in self.fields:                                                                                                                               
              self.reqparse.add_argument(f,                                                                                                                   
                                         type=str,                                                                                                            
                                         location='json')                                                                                                     
          super(ListAPI, self).__init__()                                                                                                                     
                                                                                                                                                              
      def get(self):                                                                                                                                          
          &quot;&quot;&quot;Returns all lists.&quot;&quot;&quot;                                                                                                                            
          try:                                                                                                                                                
              alllists = Voterlist.query.all()                                                                                                                
              lists = []                                                                                                                                      
              for l in alllists:                                                                                                                              
                  l = l.__dict__                                                                                                                              
                  l.pop('_sa_instance_state', None)                                                                                                           
                  lists.append(l)                                                                                                                             
              return jsonify({'lists': lists})                                                                                                                
          except Exception as e:                                                                                                                              
              print(e)                                                                                                                                        
              abort(400, str(e)) 
  </code></pre>
  <p>You can see that when the API method calls <code>Voterlist.query.all()</code> it works and even clearly includes the <code>_sa_instance_state</code> attribute, or it would throw the same error when I pop that attribute from each object in the list.</p>
  <p>Here's the definition of the Voterlist object:</p>
  <pre><code>class Voterlist(db.Model):
      id = db.Column(db.Integer, primary_key=True, default=get_time)
      description = db.Column(db.String(128))
      vendorquery = db.Column(db.String(128))
      campaign = db.Column(db.String(128))
      s3key = db.Column(db.String(128))
      fname = db.Column(db.String(128))
      ftimestamp = db.Column(db.Integer)
      source = db.Column(db.String(128))
      user = db.Column(db.String(128))
      status = db.Column(db.Integer, default=0)
  
      def __repr__(self):
          return '&lt;List {}: {}&gt;'.format(self.id, self.campaign)
  </code></pre>
  <p>Here's the full output from pytest in case that's helpful:</p>
  <pre><code>=================================================================== test session starts ====================================================================
  platform linux -- Python 3.6.9, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
  rootdir: /home/alazar66/Downloads/data/rl/rlabs-voter-list-mgr
  plugins: anyio-2.0.2
  collected 1 item                                                                                                                                           
  
  tests/functional/test_api.py F                                                                                                                       [100%]
  
  ========================================================================= FAILURES =========================================================================
  _________________________________________________________________ TestLists.test_get_lists _________________________________________________________________
  
  self = &lt;test_api.TestLists object at 0x74c48f8ff208&gt;, test_client = &lt;FlaskClient &lt;Flask 'app'&gt;&gt;
  
      def test_get_lists(self, test_client):
          '''Test download all lists.'''
          response = test_client.get(url_for('api.lists'))
          resplists = response.json['lists']
          assert type(resplists) == list
  &gt;       dblists = Voterlist.query.all()
  
  tests/functional/test_api.py:15: 
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/orm/query.py:3373: in all
      return list(self)
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/orm/loading.py:100: in instances
      cursor.close()
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py:70: in __exit__
      with_traceback=exc_tb,
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py:182: in raise_
      raise exception
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/orm/loading.py:80: in instances
      rows = [proc(row) for row in fetch]
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/orm/loading.py:80: in &lt;listcomp&gt;
      rows = [proc(row) for row in fetch]
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  
  row = (1609879216, 'Test List 0', None, None, None, None, None, None, None, 0)
  
      def _instance(row):
      
          # determine the state that we'll be populating
          if refresh_identity_key:
              # fixed state that we're refreshing
              state = refresh_state
              instance = state.obj()
              dict_ = instance_dict(instance)
              isnew = state.runid != runid
              currentload = True
              loaded_instance = False
          else:
              # look at the row, see if that identity is in the
              # session, or we have to create a new one
              identitykey = (
                  identity_class,
                  tuple([row[column] for column in pk_cols]),
                  identity_token,
              )
      
              instance = session_identity_map.get(identitykey)
      
              if instance is not None:
                  # existing instance
  &gt;               state = instance_state(instance)
  E               AttributeError: 'Voterlist' object has no attribute '_sa_instance_state'
  
  /home/alazar66/.local/lib/python3.6/site-packages/sqlalchemy/orm/loading.py:532: AttributeError
  ================================================================= short test summary info ==================================================================
  FAILED tests/functional/test_api.py::TestLists::test_get_lists - AttributeError: 'Voterlist' object has no attribute '_sa_instance_state'
  ==================================================================== 1 failed in 3.89s =====================================================================```
  </code></pre>