Skip to content

Commit 0d1944e

Browse files
Fix weakref.finalize type arguments + test max_total_rows plumbing
Cycle 10 — mypy --strict flagged weakref.finalize as needing type arguments. (run-tests.sh only runs mypy on the wire package, so the error escaped earlier cycles; caught during cycle-10 audit.) Corrected to weakref.finalize[Any, Any]. Added tests that verify max_total_rows propagates from the dbapi Connection / AsyncConnection through to the underlying DqliteConnection, using a MagicMock patch to assert the kwarg reaches the constructor. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 501c0d3 commit 0d1944e

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/dqlitedbapi/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def __init__(
9999
# close() flips to True. Using a list avoids the finalizer
100100
# closing over ``self`` and preventing GC.
101101
self._closed_flag: list[bool] = [False]
102-
self._finalizer: weakref.finalize | None = None
102+
self._finalizer: weakref.finalize[Any, Any] | None = None
103103

104104
def _check_thread(self) -> None:
105105
"""Raise ProgrammingError if called from a different thread than the creator."""
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""max_total_rows plumbing from Connection → DqliteConnection → Protocol.
2+
3+
Cycle 9 added max_total_rows to every layer. This test verifies that
4+
a custom cap set on the DBAPI Connection actually propagates down to
5+
the protocol, so users can't silently end up with the default.
6+
"""
7+
8+
import asyncio
9+
10+
from dqlitedbapi.aio.connection import AsyncConnection
11+
from dqlitedbapi.connection import Connection
12+
13+
14+
class TestMaxTotalRowsPropagation:
15+
def test_default_on_connection(self) -> None:
16+
conn = Connection("localhost:19001", timeout=2.0)
17+
try:
18+
assert conn._max_total_rows == 10_000_000
19+
finally:
20+
conn.close()
21+
22+
def test_custom_cap_on_connection(self) -> None:
23+
conn = Connection("localhost:19001", timeout=2.0, max_total_rows=42)
24+
try:
25+
assert conn._max_total_rows == 42
26+
finally:
27+
conn.close()
28+
29+
def test_none_disables_cap(self) -> None:
30+
conn = Connection("localhost:19001", timeout=2.0, max_total_rows=None)
31+
try:
32+
assert conn._max_total_rows is None
33+
finally:
34+
conn.close()
35+
36+
def test_async_connection_default(self) -> None:
37+
conn = AsyncConnection("localhost:19001", timeout=2.0)
38+
assert conn._max_total_rows == 10_000_000
39+
40+
def test_async_connection_custom(self) -> None:
41+
conn = AsyncConnection("localhost:19001", timeout=2.0, max_total_rows=7)
42+
assert conn._max_total_rows == 7
43+
44+
def test_propagates_to_underlying_dqlite_connection(self) -> None:
45+
"""After the first use, the inner DqliteConnection should see the
46+
same cap as the dbapi-level Connection."""
47+
from unittest.mock import AsyncMock, patch
48+
49+
with patch("dqlitedbapi.connection.DqliteConnection") as MockConn:
50+
instance = AsyncMock()
51+
instance.connect = AsyncMock()
52+
MockConn.return_value = instance
53+
conn = Connection("localhost:19001", timeout=2.0, max_total_rows=123)
54+
try:
55+
56+
async def warm_up() -> None:
57+
await conn._get_async_connection()
58+
59+
conn._run_sync(warm_up())
60+
# DqliteConnection was constructed with max_total_rows=123
61+
_args, kwargs = MockConn.call_args
62+
assert kwargs["max_total_rows"] == 123
63+
finally:
64+
conn.close()

0 commit comments

Comments
 (0)