psycopg / psycopg2

PostgreSQL database adapter for the Python programming language

Home Page:https://www.psycopg.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it unnecessary to format query if args is empty sequence or empty dict

vimiix opened this issue · comments

This is a bug tracker
If you have a question, such has "how do you do X with Python/PostgreSQL/psycopg2" please write to the mailing list or open a question instead.

Please complete the following information:

  • OS: Rocky Linux 8.8 (Green Obsidian)
  • Psycopg version: '2.9.6 (dt dec pq3 ext lo64)'
  • Python version: 3.9.17
  • PostgreSQL version: 15
  • pip version: 23.0.1

Describe the bug

I am testing the use of the cur.execute api. When my SQL contains the % symbol, I expect the api can determine whether it is formatted by the number of arguments, not just compare with Py_None

>>> sql = "select '1%%s'"
>>> cursor.execute(sql)
>>> cursor.fetchone()
('1%%s',)
>>> cursor.execute(sql, [])
>>> cursor.fetchone()
('1%s',) # expect ('1%%s',)

By locating code, I find that it only checked if the argument is None, and does not handle empty cases.
Can we consider compatibility with empty list or empty dict here?

No, it would be a massive non-backward incompatibility.

OK, thanks for your reply.