Skip to content

Implement inspect.signature() for sqlite3 connection object #118406

Closed

Description

@serhiy-storchaka

Feature or enhancement

sqlite3.connection() returns a custom callable object. But inspect.signature() returns a generic <Signature (*args, **kwargs)> for it:

>>> import inspect, sqlite3
>>> con = sqlite3.connect(':memory:')
>>> con('select 1')
<sqlite3.Statement object at 0x7f76842d2fd0>
>>> inspect.signature(con)
<Signature (*args, **kwargs)>

This issue is similar to #118285 and #118402.

Linked PRs

Activity

  1. erlend-aasland commented on Apr 30, 2024

    @erlend-aasland
    Contributor

    This is interesting. At first, I thought a simple patch a la this could fix it:

    Details
    diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
    index 74984ca536..f49bcd0b49 100644
    --- a/Modules/_sqlite/connection.c
    +++ b/Modules/_sqlite/connection.c
    @@ -2561,6 +2561,13 @@ set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
         return 0;
     }
     
    +#include "clinic/_sqlite3.connect.c.h"
    +static PyObject *
    +get_sig(pysqlite_Connection *self, void *Py_UNUSED(ctx))
    +{
    +    return PyUnicode_FromString(pysqlite_connect__doc__);
    +}
    +
     
     static const char connection_doc[] =
     PyDoc_STR("SQLite database connection object.");
    @@ -2570,6 +2577,7 @@ static PyGetSetDef connection_getset[] = {
         {"total_changes",  (getter)pysqlite_connection_get_total_changes, (setter)0},
         {"in_transaction",  (getter)pysqlite_connection_get_in_transaction, (setter)0},
         {"autocommit",  (getter)get_autocommit, (setter)set_autocommit},
    +    {"__text_signature__", (getter)get_sig, (setter)0},
         {NULL}
     };
     

    However, inspect.signature() will not accept that whole docstring. Manually spelling out only the signature part does not work either. Do you have a solution in hand?

  2. serhiy-storchaka commented on Apr 30, 2024

    @serhiy-storchaka
    MemberAuthor

    Simply return "(sql)" (or "(sql, /)"?).

    I have not solved this simple issue only because it needs tests, and searching in which of many of test_sqlite3 files is more suitable takes time.

  3. serhiy-storchaka commented on Apr 30, 2024

    @serhiy-storchaka
    MemberAuthor

    See also #118427 for example.

  4. erlend-aasland commented on Apr 30, 2024

    @erlend-aasland
    Contributor

    Ah, I used the incorrect signature. I'll propose a PR soon.

  5. added a commit that references this issue on Apr 30, 2024
  6. added a commit that references this issue on Apr 30, 2024
  7. added a commit that references this issue on May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions