Skip to content

Commit a5e7ee7

Browse files
feat(cursor): expose PEP 249 optional Cursor.connection attribute
Add a read-only ``connection`` property on both Cursor and AsyncCursor returning the parent connection. PEP 249's optional DB API extension section specifies this attribute; downstream libraries (e.g. pooling layers) expect it to work. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1525da0 commit a5e7ee7

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/dqlitedbapi/aio/cursor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ def __init__(self, connection: "AsyncConnection") -> None:
3131
self._closed = False
3232
self._lastrowid: int | None = None
3333

34+
@property
35+
def connection(self) -> "AsyncConnection":
36+
"""The AsyncConnection this cursor was created from.
37+
38+
PEP 249 optional extension. Read-only.
39+
"""
40+
return self._connection
41+
3442
@property
3543
def description(
3644
self,

src/dqlitedbapi/cursor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ def __init__(self, connection: "Connection") -> None:
134134
self._closed = False
135135
self._lastrowid: int | None = None
136136

137+
@property
138+
def connection(self) -> "Connection":
139+
"""The Connection this Cursor was created from.
140+
141+
PEP 249 optional extension. Read-only.
142+
"""
143+
return self._connection
144+
137145
@property
138146
def description(
139147
self,

tests/test_async_cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ async def test_close_is_idempotent(self) -> None:
4949
await cursor.close() # must not raise
5050
assert cursor._closed
5151

52+
def test_connection_property(self) -> None:
53+
conn = AsyncConnection("localhost:9001")
54+
cursor = AsyncCursor(conn)
55+
assert cursor.connection is conn
56+
5257
@pytest.mark.asyncio
5358
async def test_fetchone_on_closed_cursor_raises(self) -> None:
5459
conn = AsyncConnection("localhost:9001")

tests/test_cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def test_close_is_idempotent(self) -> None:
4242
cursor.close() # must not raise
4343
assert cursor._closed
4444

45+
def test_connection_property(self) -> None:
46+
conn = Connection("localhost:9001")
47+
cursor = Cursor(conn)
48+
assert cursor.connection is conn
49+
4550
def test_fetchone_on_closed_cursor_raises(self) -> None:
4651
conn = Connection("localhost:9001")
4752
cursor = Cursor(conn)

0 commit comments

Comments
 (0)