This project is archived and is in readonly mode.
execute & mogrify don't fail with no format placeholder but with arguments given
-
Daniele Varrazzo
- Milestone set to psycopg3
About problem 1: if the query contains no placeholder I'd argue it should also allow an empty list as argument (it is a sequence with the same number of item as the number of the placeholder in the query). It should also allow any dictionary because the set of keys in the query string is always a subset (it's empty) of the keys in the args dictionary. Hence the only thing psycopg should reject is a non-empty sequence with queries with no placeholder. I agree it's a defect but I don't know if it's worth adding an explicit check to fix it.
About problem 2, I don't think it is related, no. I think it is related to a different problem which actually bothers me more: psycopg doesn't parse the %-escaping at all if no parameter is specified. This becomes evident as soon as the % operator is used: when no parameter is specified % works ok; if a parameter is specified it must be escaped as %%. Arguably the %-escaping should be uniform. Unfortunately now it's too late to fix it: every query using an operator including % and no param should be rewritten (probably there aren't many in the wild, but a few are definitely there). I would fix this problem only in a non-100%-backward-compatible psycopg3 (or was 100%%?).
Note that the workaround for the not uniform %% escaping is exactly to pass an empty parameters list to the query.
>>> cur.execute("select '%%s'") >>> cur.fetchone()[0] '%%s' >>> cur.execute("select '%%s'", []) >>> cur.fetchone()[0] '%s'I will leave this bug open as an idea to fix in psycopg3, but probably won't do anything in psycopg2 for backward compatibility reason.