Skip to content

Commit

Permalink
gh-108278: Deprecate passing the first param of sqlite3.Connection ca…
Browse files Browse the repository at this point in the history
…llback APIs by keyword (#108632)

Deprecate passing the callback callable by keyword for the following
sqlite3.Connection APIs:

- set_authorizer(authorizer_callback)
- set_progress_handler(progress_handler, ...)
- set_trace_callback(trace_callback)

The affected parameters will become positional-only in Python 3.15.
  • Loading branch information
erlend-aasland committed Aug 29, 2023
1 parent 77e8f23 commit 0b0c1d0
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 22 deletions.
27 changes: 21 additions & 6 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,10 @@ Connection objects
... print(row)
('acbd18db4cc2f85cedef654fccc4a4d8',)

.. versionchanged:: 3.13
.. versionchanged:: 3.13

Passing *name*, *narg*, and *func* as keyword arguments is deprecated.
These parameters will become positional-only in Python 3.15.
Passing *name*, *narg*, and *func* as keyword arguments is deprecated.
These parameters will become positional-only in Python 3.15.


.. method:: create_aggregate(name, n_arg, aggregate_class)
Expand Down Expand Up @@ -822,10 +822,10 @@ Connection objects

3

.. versionchanged:: 3.13
.. versionchanged:: 3.13

Passing *name*, *n_arg*, and *aggregate_class* as keyword arguments is deprecated.
These parameters will become positional-only in Python 3.15.
Passing *name*, *n_arg*, and *aggregate_class* as keyword arguments is deprecated.
These parameters will become positional-only in Python 3.15.


.. method:: create_window_function(name, num_params, aggregate_class, /)
Expand Down Expand Up @@ -991,6 +991,11 @@ Connection objects
.. versionchanged:: 3.11
Added support for disabling the authorizer using ``None``.

.. versionchanged:: 3.13

Passing *authorizer_callback* as a keyword argument to is deprecated.
The parameter will become positional-only in Python 3.15.


.. method:: set_progress_handler(progress_handler, n)

Expand All @@ -1006,6 +1011,11 @@ Connection objects
currently executing query and cause it to raise a :exc:`DatabaseError`
exception.

.. versionchanged:: 3.13

Passing *progress_handler* as a keyword argument to is deprecated.
The parameter will become positional-only in Python 3.15.


.. method:: set_trace_callback(trace_callback)

Expand All @@ -1030,6 +1040,11 @@ Connection objects

.. versionadded:: 3.3

.. versionchanged:: 3.13

Passing *trace_callback* as a keyword argument to is deprecated.
The parameter will become positional-only in Python 3.15.


.. method:: enable_load_extension(enabled, /)

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ Deprecated
* :meth:`~sqlite3.Connection.create_function`
* :meth:`~sqlite3.Connection.create_aggregate`

Deprecate passing the callback callable by keyword for the following
:class:`sqlite3.Connection` APIs:

* :meth:`~sqlite3.Connection.set_authorizer`
* :meth:`~sqlite3.Connection.set_progress_handler`
* :meth:`~sqlite3.Connection.set_trace_callback`

The affected parameters will become positional-only in Python 3.15.

(Contributed by Erlend E. Aasland in :gh:`107948` and :gh:`108278`.)
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_sqlite3/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ def bad_progress():
create table foo(a, b)
""")

def test_progress_handler_keyword_args(self):
regex = (
r"Passing keyword argument 'progress_handler' to "
r"_sqlite3.Connection.set_progress_handler\(\) is deprecated. "
r"Parameter 'progress_handler' will become positional-only in "
r"Python 3.15."
)

with self.assertWarnsRegex(DeprecationWarning, regex) as cm:
self.con.set_progress_handler(progress_handler=lambda: None, n=1)
self.assertEqual(cm.filename, __file__)


class TraceCallbackTests(MemoryDatabaseMixin, unittest.TestCase):

Expand Down Expand Up @@ -340,6 +352,18 @@ def test_trace_bad_handler(self):
cx.set_trace_callback(lambda stmt: 5/0)
cx.execute("select 1")

def test_trace_keyword_args(self):
regex = (
r"Passing keyword argument 'trace_callback' to "
r"_sqlite3.Connection.set_trace_callback\(\) is deprecated. "
r"Parameter 'trace_callback' will become positional-only in "
r"Python 3.15."
)

with self.assertWarnsRegex(DeprecationWarning, regex) as cm:
self.con.set_trace_callback(trace_callback=lambda: None)
self.assertEqual(cm.filename, __file__)


if __name__ == "__main__":
unittest.main()
33 changes: 33 additions & 0 deletions Lib/test/test_sqlite3/test_userfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,27 @@ def test_aggr_text(self):
val = cur.fetchone()[0]
self.assertEqual(val, txt)

def test_agg_keyword_args(self):
regex = (
r"Passing keyword arguments 'name', 'n_arg' and 'aggregate_class' to "
r"_sqlite3.Connection.create_aggregate\(\) is deprecated. "
r"Parameters 'name', 'n_arg' and 'aggregate_class' will become "
r"positional-only in Python 3.15."
)

with self.assertWarnsRegex(DeprecationWarning, regex) as cm:
self.con.create_aggregate("test", 1, aggregate_class=AggrText)
self.assertEqual(cm.filename, __file__)

with self.assertWarnsRegex(DeprecationWarning, regex) as cm:
self.con.create_aggregate("test", n_arg=1, aggregate_class=AggrText)
self.assertEqual(cm.filename, __file__)

with self.assertWarnsRegex(DeprecationWarning, regex) as cm:
self.con.create_aggregate(name="test", n_arg=0,
aggregate_class=AggrText)
self.assertEqual(cm.filename, __file__)


class AuthorizerTests(unittest.TestCase):
@staticmethod
Expand Down Expand Up @@ -779,6 +800,18 @@ def test_clear_authorizer(self):
self.con.execute("select * from t2")
self.con.execute("select c2 from t1")

def test_authorizer_keyword_args(self):
regex = (
r"Passing keyword argument 'authorizer_callback' to "
r"_sqlite3.Connection.set_authorizer\(\) is deprecated. "
r"Parameter 'authorizer_callback' will become positional-only in "
r"Python 3.15."
)

with self.assertWarnsRegex(DeprecationWarning, regex) as cm:
self.con.set_authorizer(authorizer_callback=lambda: None)
self.assertEqual(cm.filename, __file__)


class AuthorizerRaiseExceptionTests(AuthorizerTests):
@staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Deprecate passing the callback callable by keyword for the following
:class:`sqlite3.Connection` APIs:

* :meth:`~sqlite3.Connection.set_authorizer`
* :meth:`~sqlite3.Connection.set_progress_handler`
* :meth:`~sqlite3.Connection.set_trace_callback`

The affected parameters will become positional-only in Python 3.15.

Patch by Erlend E. Aasland.
102 changes: 95 additions & 7 deletions Modules/_sqlite/clinic/connection.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b0c1d0

Please sign in to comment.