flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Possible issue during getting engine for bind with python in optimized mode (PYTHONOPTIMIZE=1)

Open krasoffski opened this issue 5 years ago • 0 comments

flask-sqlalchemy returns meaningless error when trying to get engine for bind when configuration does not contain any binds for python in optimized mode (assertions is not evaluated).

  File "replica.py", line 66, in replica_session
    engine = self._db.get_engine(bind='replica')
  File "/app/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
    return connector.get_engine()
  File "/app/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 549, in get_engine
    uri = self.get_uri()
  File "/app/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 545, in get_uri
    return binds[self._bind]
TypeError: tuple indices must be integers or slices, not str

Quick investigation showed that default value when no binds found is tuple but on the last line expected a dict: source code

Expected behavior is following: Remove assertion and use specific error for this or keep assertion but replace default value with empty dict like: binds = self._app.config.get('SQLALCHEMY_BINDS') or {} because documentation shows that this value should be a dict:

SQLALCHEMY_BINDS = {
    'users':        'mysqldb://localhost/users',
    'appmeta':      'sqlite:////path/to/appmeta.db'
}

krasoffski avatar Mar 16 '20 09:03 krasoffski