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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up sqlite3.Connection APIs #108278

Closed
erlend-aasland opened this issue Aug 22, 2023 · 8 comments
Closed

Clean up sqlite3.Connection APIs #108278

erlend-aasland opened this issue Aug 22, 2023 · 8 comments
Labels
topic-sqlite3 type-feature A feature request or enhancement

Comments

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Aug 22, 2023

Feature or enhancement

Has this already been discussed elsewhere?

https://discuss.python.org/t/clean-up-some-sqlite3-apis/32093

Links to previous discussion of this feature:

#87260 (comment)

Proposal:

The sqlite3.Connection class has groups of similar APIs with inconsistent parameter specs.

Create user-defined function APIs:

  • create_function(name, narg, func, *, deterministic=False)
  • create_aggregate(name, /, n_arg, aggregate_class)
  • create_window_function(name, num_params, aggregate_class, /)
  • create_collation(name, callable, /)

Set callback APIs:

  • set_authorizer(authorizer_callback)
  • set_progress_handler(progress_handler, n)
  • set_trace_callback(trace_callback)

For all APIs but create_function, I suggest to make all parameters positional-only; for create_function, I suggest to make the three first parameters positional-only:

Create user-defined function APIs:

  • create_function(name, nargs, callable, /, *, deterministic=False)
  • create_aggregate(name, nargs, aggregate_class, /)
  • create_window_function(name, nargs, aggregate_class, /)
  • create_collation(name, callable, /)

Set callback APIs:

  • set_authorizer(authorizer_callback, /)
  • set_progress_handler(progress_handler, /, n)
  • set_trace_callback(trace_callback, /)

Obviously, create_window_function stays as it is.

UPDATE: I noticed the docs are wrong about create_collation; it's signature is actually create_collation(name, callable, /).

Linked PRs

@erlend-aasland erlend-aasland added type-feature A feature request or enhancement topic-sqlite3 labels Aug 22, 2023
@erlend-aasland
Copy link
Contributor Author

cc. @serhiy-storchaka

erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Aug 22, 2023
Deprecate passing name, number of arguments, and the callable as keyword
arguments, for the following sqlite3.Connection APIs:

- create_function(name, nargs, callable, ...)
- create_aggregate(name, nargs, callable)

Deprecate passing the callback as a keyword argument, for the following
sqlite3.Connection APIs:

- set_authorizer(callback)
- set_progress_handler(callback, n)
- set_trace_callback(callback)

The affected parameters will become positional-only in Python 3.15.
@erlend-aasland erlend-aasland changed the title Clean up sqlite3 APIs Clean up sqlite3.Connection APIs Aug 22, 2023
@serhiy-storchaka
Copy link
Member

Why keep n in set_progress_handler() as keyword-or-positional parameter? It is not very expressive name.

@erlend-aasland
Copy link
Contributor Author

Why keep n in set_progress_handler() as keyword-or-positional parameter? It is not very expressive name.

I did consider it, and I'm still considering it :) I'm not sure what makes for the better API.

  1. con.set_progress_handler(func, 10)
  2. con.set_progress_handler(func, n=10)

@serhiy-storchaka
Copy link
Member

Or maybe con.set_progress_handler(func, num_ops=10)? n may not be the best name.

On other hand, set_progress_handler() has the order of parameters opposite to sqlite3_progress_handler(), it can cause confusion, so someone can call it as con.set_progress_handler(n=10, progress_handler=func).

So I am not sure about these changes. On other hand, having different names narg, n_arg and num_params for virtually the same parameter is ridiculous. And name which can or cannot be passed by keyword. Ad func and callable which has virtually the same meaning in Python. So I agree with the first part of changes.

Alternatively we can rename keyword arguments with keeping deprecated alias (#108271), but first we need to agree on names. It is easier to simply deprecate passing by keyword.

@erlend-aasland
Copy link
Contributor Author

Yes, it is easier to deprecate passing by keyword, and I also think it makes for a cleaner API.

Regarding similarity to SQLite C API; I'm not sure it makes sense to align our APIs with those of SQLite. The sqlite3 extension module is a DB API implementation; it does not aim to be a Python shim over the SQLite C API (like apsw).

I'll split my PR in two, so there is one PR for each API group; it will be easier to reason about.

@erlend-aasland
Copy link
Contributor Author

n may not be the best name.

I agree; generally, I'm not a big fan of 1-letter variable/parameter names. num_ops or nops would be better. As you say, we'd need to resolve #108271 if we want to be able to rename a keyword parameter.

erlend-aasland added a commit that referenced this issue Aug 28, 2023
…or sqlite3 UDF creation APIs (#108281)

Deprecate passing name, number of arguments, and the callable as keyword
arguments, for the following sqlite3.Connection APIs:

- create_function(name, nargs, callable, ...)
- create_aggregate(name, nargs, callable)

The affected parameters will become positional-only in Python 3.15.
vstinner pushed a commit to vstinner/cpython that referenced this issue Aug 28, 2023
…args for sqlite3 UDF creation APIs (python#108281)

Deprecate passing name, number of arguments, and the callable as keyword
arguments, for the following sqlite3.Connection APIs:

- create_function(name, nargs, callable, ...)
- create_aggregate(name, nargs, callable)

The affected parameters will become positional-only in Python 3.15.
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Aug 29, 2023
…ion callback APIs by keyword

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.
@erlend-aasland
Copy link
Contributor Author

@serhiy-storchaka, I created a PR for the callback handlers. You raised a question regarding the n parameter of set_progress_handler, and also regarding the similarity to the SQLite C API.

Regarding the latter, I already replied that I don't think the order of parameters in the SQLite C API should be directing the order of parameters in the sqlite3 API; the sqlite3 module is not apsw.

Regarding my suggested omission of the n parameters in set_progress_handler, I am not sure what makes for the best API:

  1. set_progress_handler(func, n=100)
  2. set_progress_handler(func, 100)

n is a single letter; one can argue that it does not convey a lot of meaning; n will probably be interpreted as meaning "count", but it is still unknown what is counted :) Obviously ninstr or num_instructions would have been a better choice. The easiest solution is to make it a positional parameter instead.

Also, the docstring of set_progress_handler can be greatly improved, so perhaps n is fine, as long as the docstring explains it.

erlend-aasland added a commit that referenced this issue Aug 29, 2023
…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.
@erlend-aasland
Copy link
Contributor Author

Thanks for your input and reviews, Serhiy.

carljm added a commit to carljm/cpython that referenced this issue Aug 30, 2023
* main:
  pythongh-108520: Fix bad fork detection in nested multiprocessing use case (python#108568)
  pythongh-108590: Revert pythongh-108657 (commit 400a1ce) (python#108686)
  pythongh-108494: Argument Clinic: Document how to generate code that uses the limited C API (python#108584)
  Document Python build requirements (python#108646)
  pythongh-101100: Fix Sphinx warnings in the Logging Cookbook (python#108678)
  Fix typo in multiprocessing docs (python#108666)
  pythongh-108669: unittest: Fix documentation for TestResult.collectedDurations (python#108670)
  pythongh-108590: Fix sqlite3.iterdump for invalid Unicode in TEXT columns (python#108657)
  Revert "pythongh-103224: Use the realpath of the Python executable in `test_venv` (pythonGH-103243)" (pythonGH-108667)
  pythongh-106320: Remove private _Py_ForgetReference() (python#108664)
  Mention Ellipsis pickling in the docs (python#103660)
  Revert "Use non alternate name for Kyiv (pythonGH-108533)" (pythonGH-108649)
  pythongh-108278: Deprecate passing the first param of sqlite3.Connection callback APIs by keyword (python#108632)
  pythongh-108455: peg_generator: install two stubs packages before running mypy (python#108637)
  pythongh-107801: Improve the accuracy of io.IOBase.seek docs (python#108268)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-sqlite3 type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

2 participants