The objects exposed by the sql module can be used to compose a query as a Python string (using the as_string() method) or passed directly to cursor methods such as execute(), executemany(), copy_expert().
Current psycopg2 wrapper based on _dbapi2 that provide common implementation of wrappers for connections / cursors, that's why it doesn't (and maybe shouldn't) supporting something different from the string sql statement.
____________________________ test_execute_sql[SQL] _____________________________
tests/opentracing_instrumentation/test_postgres.py:161: in test_execute_sql
cur.execute(query, (trace_id, ))
opentracing_instrumentation/client_hooks/_dbapi2.py:204: in execute
cursor_params=self._cursor_params):
opentracing_instrumentation/client_hooks/_dbapi2.py:56: in db_span
statement = sql_statement.strip()
E AttributeError: 'SQL' object has no attribute 'strip'
--------------------------- Captured stdout teardown ---------------------------
F
__________________________ test_execute_sql[Composed] __________________________
tests/opentracing_instrumentation/test_postgres.py:161: in test_execute_sql
cur.execute(query, (trace_id, ))
opentracing_instrumentation/client_hooks/_dbapi2.py:204: in execute
cursor_params=self._cursor_params):
opentracing_instrumentation/client_hooks/_dbapi2.py:56: in db_span
statement = sql_statement.strip()
E AttributeError: 'Composed' object has no attribute 'strip'
--------------------------- Captured stdout teardown ---------------------------
F
In Psycopg2 documentation mentioned that
execute
method allowsComposable
object as well as a string (http://initd.org/psycopg/docs/sql.html):Current psycopg2 wrapper based on
_dbapi2
that provide common implementation of wrappers for connections / cursors, that's why it doesn't (and maybe shouldn't) supporting something different from the string sql statement.I made one test to illustrate the problem: https://github.com/condorcet/opentracing-python-instrumentation/blob/psycopg2-sql-as-composable-error/tests/opentracing_instrumentation/test_postgres.py#L149 And you can see results on CI: https://www.travis-ci.org/condorcet/opentracing-python-instrumentation/jobs/533795695#L525