classification
Title: Use more descriptive variable names in sqlite3 docs
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: berker.peksag, docs@python, erlendaasland, miss-islington, toreanderson
Priority: normal Keywords: patch

Created on 2021-03-04 06:44 by toreanderson, last changed 2021-03-04 17:13 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
patch.diff erlendaasland, 2021-03-04 15:00
Pull Requests
URL Status Linked Edit
PR 24745 closed erlendaasland, 2021-03-04 10:39
PR 24746 merged erlendaasland, 2021-03-04 15:28
PR 24747 merged miss-islington, 2021-03-04 15:47
PR 24748 merged miss-islington, 2021-03-04 15:47
Messages (14)
msg388078 - (view) Author: Tore Anderson (toreanderson) Date: 2021-03-04 06:44
In https://docs.python.org/3/library/sqlite3.html, the following example code is found:

> # Do this instead
> t = ('RHAT',)
> c.execute('SELECT * FROM stocks WHERE symbol=?', t)
> print(c.fetchone())

However this fails as follows:

> Traceback (most recent call last):
>   File "./test.py", line 8, in <module>
>     print(c.fetchone())
> AttributeError: 'sqlite3.Connection' object has no attribute 'fetchone'

I believe the correct code should have been (at least it works for me):

> # Do this instead
> t = ('RHAT',)
> cursor = c.execute('SELECT * FROM stocks WHERE symbol=?', t)
> print(cursor.fetchone())

Tore
msg388081 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-03-04 10:35
Correct, fetchone() is a cursor method. Thanks for the report!
msg388085 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 13:46
Could you please post the full snippet?

c is already set to a cursor at https://github.com/python/cpython/blame/e161ec5dd7ba9355eb06757b9304019ac53cdf69/Doc/library/sqlite3.rst#L56:

    c = conn.cursor()

And the following example works fine:

    >>> import sqlite3 as s
    >>> conn = s.connect(":memory:")
    >>> c = conn.cursor()
    >>> c.execute("select 1")
    <sqlite3.Cursor object at 0x00000291A5A1F960>
    >>> c.fetchone()
    (1,)
msg388086 - (view) Author: Tore Anderson (toreanderson) Date: 2021-03-04 13:48
You're looking in the wrong place, the buggy ones are at https://github.com/python/cpython/blame/e161ec5dd7ba9355eb06757b9304019ac53cdf69/Doc/library/sqlite3.rst#L74-L76

Tore
msg388087 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 13:55
https://github.com/python/cpython/blame/e161ec5dd7ba9355eb06757b9304019ac53cdf69/Doc/library/sqlite3.rst#L74-L76 is not a standalone snippet. It uses the cursor object created at https://github.com/python/cpython/blame/e161ec5dd7ba9355eb06757b9304019ac53cdf69/Doc/library/sqlite3.rst#L54-L56 So there is nothing wrong here.

Maybe we can rename c to cur to make it more readable and harder to be confused with a connection object.
msg388088 - (view) Author: Tore Anderson (toreanderson) Date: 2021-03-04 14:18
You're right. I got it confused with the conn object in the code I was working on, because it turns out that it has an execute() method:

>>> import sqlite3
>>> c = sqlite3.connect('test.db')
>>> c.execute('SELECT * FROM tbl')
<sqlite3.Cursor object at 0x7f6dc7a07c00>

Closing, apologies for the noise!
msg388089 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-03-04 14:20
Right, got me confused as well! I guess a clarification would be an improvement :)
msg388092 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 14:51
No problem and thank you for taking time to report!

I'd be happy to merge a PR that renames c to cur in the documentation (I counted four instances.) We can then reopen and retarget this isssue.
msg388093 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-03-04 14:56
'con' and 'cur' seems to be used in a majority of the examples. I suggest normalising to always using these two in code examples.
msg388094 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-03-04 15:00
Attached patch consists of two commits: one to rename "conn" to "con", and one to rename "c" to "cur". Are you ok with that, Berker?
msg388096 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 15:24
LGTM!
msg388097 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 15:46
New changeset 40d1b831ecd1b5b6a4fce9a908a6e61b50b360a0 by Erlend Egeberg Aasland in branch 'master':
bpo-43396: Normalise naming in sqlite3 doc examples (GH-24746)
https://github.com/python/cpython/commit/40d1b831ecd1b5b6a4fce9a908a6e61b50b360a0
msg388106 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 17:11
New changeset 374ee449331bc95d18c37f5032aaea1448462e58 by Miss Islington (bot) in branch '3.9':
bpo-43396: Normalise naming in sqlite3 doc examples (GH-24746)
https://github.com/python/cpython/commit/374ee449331bc95d18c37f5032aaea1448462e58
msg388107 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-03-04 17:12
New changeset 213c155a460b8dd9e43901e4d61aa088cbac4221 by Miss Islington (bot) in branch '3.8':
bpo-43396: Normalise naming in sqlite3 doc examples (GH-24746)
https://github.com/python/cpython/commit/213c155a460b8dd9e43901e4d61aa088cbac4221
History
Date User Action Args
2021-03-04 17:13:49berker.peksagsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.8
2021-03-04 17:12:14berker.peksagsetmessages: + msg388107
2021-03-04 17:11:49berker.peksagsetmessages: + msg388106
2021-03-04 15:47:16miss-islingtonsetpull_requests: + pull_request23518
2021-03-04 15:47:04miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23517
2021-03-04 15:46:21berker.peksagsetmessages: + msg388097
2021-03-04 15:28:38erlendaaslandsetstage: needs patch -> patch review
pull_requests: + pull_request23516
2021-03-04 15:24:58berker.peksagsetstatus: closed -> open

title: Non-existent method sqlite3.Connection.fetchone() used in docs -> Use more descriptive variable names in sqlite3 docs
messages: + msg388096
versions: + Python 3.10
type: enhancement
resolution: not a bug -> (no value)
stage: resolved -> needs patch
2021-03-04 15:00:24erlendaaslandsetfiles: + patch.diff

messages: + msg388094
2021-03-04 14:56:14erlendaaslandsetmessages: + msg388093
2021-03-04 14:51:46berker.peksagsetresolution: not a bug
messages: + msg388092
2021-03-04 14:20:34erlendaaslandsetmessages: + msg388089
2021-03-04 14:18:56toreandersonsetstatus: open -> closed

messages: + msg388088
stage: patch review -> resolved
2021-03-04 13:55:07berker.peksagsetmessages: + msg388087
2021-03-04 13:48:15toreandersonsetmessages: + msg388086
2021-03-04 13:46:03berker.peksagsetmessages: + msg388085
2021-03-04 10:39:59erlendaaslandsetkeywords: + patch
stage: patch review
pull_requests: + pull_request23515
2021-03-04 10:35:43erlendaaslandsetnosy: + berker.peksag, erlendaasland
messages: + msg388081
2021-03-04 06:44:55toreandersoncreate