Skip to content
New issue

Have a question about this project?Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101693: In sqlite3, deprecate using named placeholders with parameters supplied as a sequence #101698

Merged
Merged
Show file tree
Hide file tree
Changes from8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,11 @@ Cursor objects
and there is no open transaction,
a transaction is implicitly opened before executing *sql*.

.. versionchanged:: 3.12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
..versionchanged::3.12
..deprecated-removed::3.123.14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Are you sure about that? The API itself is not deprecated, we're just changing it. I'm not sure what's the recommended practice here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, I know what you mean. I feel like ideally this notice would be phrased along the lines of "X behaviour/practice is now deprecated" rather than "a DeprecationWarning is now emitted". That would be more to-the-point, and it would also work more naturally with this directive (X behaviour/practice is deprecated in 3.12, and will be removed entirely in 3.14).

But I was struggling to come to with a concrete suggestion for how to reword these notices:/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

(The whatsnew and NEWS entries look great btw, it's just the notices in the API docs that feelslightlyclunky to me)

Copy link
Contributor Author

@erlend-aasland erlend-aasland Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I feel like ideally this notice would be phrased along the lines of "X behaviour/practice is now deprecated" rather than "a DeprecationWarning is now emitted". That would be more to-the-point, and it would also work more naturally with this directive (X behaviour/practice is deprecated in 3.12, and will be removed entirely in 3.14).

Yes, but documented as deprecated and emitting aDeprecationWarningare similar, but not equal, things 🙂 With the former, we don't need to emit a warning in the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

To your original question, btw: I think it's pretty standard to use.. deprecatedor.. deprecated-removed,even if it's just a particular usage of an API, rather than the API itself. See e.g.https://docs.python.org/3.12/library/asyncio-policy.html#asyncio.DefaultEventLoopPolicy,where the directive is used even though the class itself hasn't been deprecated at all; or#19867,which deprecated just a specific parameter; or lots of other examples in our docs 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Aight! I'll try to reword it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I've updated it to usedeprecated-removed,and I put in an extra line regarding what happens in 3.14. I'm too tired to reword the text 🙂


:exc:`DeprecationWarning` is raised if
:ref:`named placeholders <sqlite3-placeholders>` are used
and *parameters* is a sequence.
erlend-aaslandmarked this conversation as resolved.
Show resolved Hide resolved

.. method:: executemany(sql, parameters, /)

Expand All @@ -1455,6 +1460,12 @@ Cursor objects
# cur is an sqlite3.Cursor object
cur.executemany( "INSERT INTO data VALUES(?)", rows)

.. versionchanged:: 3.12
erlend-aaslandmarked this conversation as resolved.
Show resolved Hide resolved

:exc:`DeprecationWarning` is raised if
:ref:`named placeholders <sqlite3-placeholders>` are used
and the items in *parameters* are sequences.

.. method:: executescript(sql_script, /)

Execute the SQL statements in *sql_script*.
Expand Down Expand Up @@ -1950,7 +1961,7 @@ question marks (qmark style) or named placeholders (named style).
For the qmark style, *parameters* must be a
:term:`sequence` whose length must match the number of placeholders,
or a:exc:`ProgrammingError` is raised.
For the named style, *parameters*shouldbe
For the named style, *parameters*mustbe
an instance of a:class:`dict` (or a subclass),
which must contain keys for all named parameters;
any extra items are ignored.
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ Deprecated
and tailor them to your needs.
(Contributed by Erlend E. Aasland in:gh:`90016`.)

* In:meth:`~sqlite3.Cursor.execute`,:exc:`DeprecationWarning` is now raised
AlexWaygoodmarked this conversation as resolved.
Show resolved Hide resolved
when:ref:`named placeholders <sqlite3-placeholders>` are used together with
parameters supplied as a:term:`sequence`.
Starting from Python 3.14, using named placeholders with parameters supplied
as a sequence will raise a:exc:`~sqlite3.ProgrammingError`.
(Contributed by Erlend E. Aasland in:gh:`101698`.)

* The 3-arg signatures (type, value, traceback) of:meth:`~coroutine.throw`,
:meth:`~generator.throw` and:meth:`~agen.athrow` are deprecated and
may be removed in a future version of Python. Use the single-arg versions
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,21 @@ def __getitem__(slf, x):
with self.assertRaises(ZeroDivisionError):
self.cu.execute( "select name from test where name=?", L())

def test_execute_named_param_and_sequence(self):
dataset = (
erlend-aaslandmarked this conversation as resolved.
Show resolved Hide resolved
( "select:a", (1,)),
( "select:a,?,?", (1, 2, 3)),
( "select?,:b,?", (1, 2, 3)),
( "select?,?,:c", (1, 2, 3)),
( "select:a,:b,?", (1, 2, 3)),
)
msg = "Binding.*is a named parameter"
for query, params in dataset:
with self.subTest(query=query, params=params):
with self.assertWarnsRegex(DeprecationWarning, msg) as cm:
self.cu.execute(query, params)
self.assertEqual(cm.filename, __file__)

def test_execute_too_many_params(self):
category = sqlite.SQLITE_LIMIT_VARIABLE_NUMBER
msg = "too many SQL variables"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
In:meth:`sqlite3.Cursor.execute`,:exc:`DeprecationWarning` is now raised
AlexWaygoodmarked this conversation as resolved.
Show resolved Hide resolved
when:ref:`named placeholders <sqlite3-placeholders>` are used together with
parameters supplied as a:term:`sequence`.
Starting from Python 3.14, using named placeholders with parameters supplied
as a sequence will raise a:exc:`~sqlite3.ProgrammingError`.
Patch by Erlend E. Aasland.
13 changes: 13 additions & 0 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,19 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
return;
}
for (i = 0; i < num_params; i++) {
const char *name = sqlite3_bind_parameter_name(self->st, i+1);
if (name!= NULL) {
int ret = PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"Binding %d ('%s') is a named parameter, but you"
"supplied a sequence which requires nameless (qmark)"
"placeholders. Starting with Python 3.14 an"
"sqlite3.ProgrammingError will be raised.",
i+1, name);
if (ret < 0) {
return;
}
}

if (PyTuple_CheckExact(parameters)) {
PyObject *item = PyTuple_GET_ITEM(parameters, i);
current_param = Py_NewRef(item);
Expand Down