A SQLAlchemy query is a generative object that you keep obtaining a modified copy of with successive method calls. Often, the final method call executes the query and returns a value. Python's built-in line continuation can obscure the structure, so explicitly use \
here.
Instead of this:
return db_session.query(Opportunity.id).filter(Opportunity.is_complete == sa.sql.true(),
Opportunity.created_by_id == flask.session.user_id,
Opportunity.is_staff_created == sa.sql.false()).count()