Database as a context manager, fixed many pytest warnings

* Database can now work as a context manager
* Claude Code helped fix a ton of .close() warnings

https://gistpreview.github.io/?730f0c5dc38528a1dd0615f330bd5481

* New autouse fixture to help with test warnings

Refs https://github.com/simonw/sqlite-utils/issues/692#issuecomment-3644371889

* Fix all remaining resource warnings

https://gistpreview.github.io/?0bb8e869b82f6ff0db647de755182502

Closes #692
This commit is contained in:
Simon Willison 2025-12-11 16:56:12 -08:00 committed by GitHub
commit fd5b09f64b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 231 additions and 66 deletions

View file

@ -2,6 +2,14 @@ from sqlite_utils.db import Index, View, Database, XIndex, XIndexColumn
import pytest
def _check_supports_strict():
"""Check if SQLite supports strict tables without leaking the database."""
db = Database(memory=True)
result = db.supports_strict
db.close()
return result
def test_table_names(existing_db):
assert ["foo"] == existing_db.table_names()
@ -282,7 +290,7 @@ def test_use_rowid(fresh_db):
@pytest.mark.skipif(
not Database(memory=True).supports_strict,
not _check_supports_strict(),
reason="Needs SQLite version that supports strict",
)
@pytest.mark.parametrize(