Type fixes now enforced by ty

* Fix type warning for pipe.stdout possibly being None

Add conditional check before calling .read() on pipe.stdout since
Popen can return None for stdout.

* Use ctx.meta instead of dynamic attribute for database cleanup

Click's Context.meta dictionary is the proper way to store arbitrary
data on the context object, avoiding type checker warnings about
dynamic attribute assignment.

* Add assert for tables.callback before calling

Click's callback attribute is typed as Optional[Callable], so add
assert to satisfy type checker that it's not None.

* Fix type errors in cli.py and db.py

- Add type annotation for Database.conn to fix context manager errors
- Convert exception objects to str() when raising ClickException
- Handle None return from find_spatialite() with proper error message

* Fix remaining type errors in cli.py

- Add typing import and type annotations for dict kwargs
- Use db.table() instead of db[] for extract command
- Fix missing str() conversion for exception

* Fix type errors in db.py

- Add type annotation for Database.conn
- Add type: ignore for optional sqlite_dump import
- Update execute/query parameter types to Sequence|Dict for sqlite3 compatibility
- Use getattr for fn.__name__ access to handle callables without __name__
- Handle None return from find_spatialite() with OSError
- Fix pk_values assignment to use local variable

* Add type: ignore for optional pysqlite3 and sqlean imports

These are alternative sqlite3 implementations that may not be installed.

* Fix type errors in tests and plugins

- Add type: ignore for monkey-patching Database.__init__ in conftest
- Fix CLI test to pass string "2" instead of integer to Click invoke
- Add type: ignore for optional sqlean import
- Fix add_geometry_column test to use "XY" instead of integer 2
- Add type: ignore for click.Context as context manager
- Add type: ignore for enable_fts test that intentionally omits argument
- Add type: ignore for sys._called_from_test dynamic attribute
- Fix rows_from_file test type error for intentional wrong argument
- Handle None from pm.get_hookcallers in plugins.py

* Use db.table() instead of db[] for Table-specific operations

Changes db[table] to db.table(table) in CLI commands where we know
we're working with tables, not views. This resolves most of the
Table | View disambiguation type warnings since db.table() returns
Table directly rather than Table | View.

* Fix remaining type warnings in sqlite_utils package

- Add assert for sniff_buffer not being None
- Handle cursor.fetchone() potentially returning None
- Use db.table() for counts_table and index_foreign_keys
- Add type: ignore for cursor union type in raw mode

* Ran Black

* Run ty in CI

* ty check sqlite_utils

* Skip running ty on Windows

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2025-12-16 19:34:45 -08:00 committed by GitHub
commit 871038505b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 89 additions and 68 deletions

View file

@ -41,7 +41,7 @@ import uuid
from sqlite_utils.plugins import pm
try:
from sqlite_dump import iterdump
from sqlite_dump import iterdump # type: ignore[import-not-found]
except ImportError:
iterdump = None
@ -328,6 +328,7 @@ class Database:
_counts_table_name = "_counts"
use_counts_table = False
conn: sqlite3.Connection
def __init__(
self,
@ -525,7 +526,7 @@ class Database:
self.execute(attach_sql)
def query(
self, sql: str, params: Optional[Union[Iterable, dict]] = None
self, sql: str, params: Optional[Union[Sequence, Dict[str, Any]]] = None
) -> Generator[dict, None, None]:
"""
Execute ``sql`` and return an iterable of dictionaries representing each row.
@ -540,7 +541,7 @@ class Database:
yield dict(zip(keys, row))
def execute(
self, sql: str, parameters: Optional[Union[Iterable, dict]] = None
self, sql: str, parameters: Optional[Union[Sequence, Dict[str, Any]]] = None
) -> sqlite3.Cursor:
"""
Execute SQL query and return a ``sqlite3.Cursor``.
@ -805,10 +806,11 @@ class Database:
:param tables: Subset list of tables to return counts for.
"""
sql = 'select "table", count from {}'.format(self._counts_table_name)
if tables:
sql += ' where "table" in ({})'.format(", ".join("?" for table in tables))
tables_list = list(tables) if tables else None
if tables_list:
sql += ' where "table" in ({})'.format(", ".join("?" for _ in tables_list))
try:
return {r[0]: r[1] for r in self.execute(sql, tables).fetchall()}
return {r[0]: r[1] for r in self.execute(sql, tables_list).fetchall()}
except OperationalError:
return {}
@ -817,7 +819,7 @@ class Database:
tables = [table for table in self.tables if table.has_counts_triggers]
with self.conn:
self._ensure_counts_table()
counts_table = self[self._counts_table_name]
counts_table = self.table(self._counts_table_name)
counts_table.delete_where()
counts_table.insert_all(
{"table": table.name, "count": table.execute_count()}
@ -825,7 +827,7 @@ class Database:
)
def execute_returning_dicts(
self, sql: str, params: Optional[Union[Iterable, dict]] = None
self, sql: str, params: Optional[Union[Sequence, Dict[str, Any]]] = None
) -> List[dict]:
return list(self.query(sql, params))
@ -1273,7 +1275,7 @@ class Database:
def index_foreign_keys(self):
"Create indexes for every foreign key column on every table in the database."
for table_name in self.table_names():
table = self[table_name]
table = self.table(table_name)
existing_indexes = {
i.columns[0] for i in table.indexes if len(i.columns) == 1
}
@ -1339,6 +1341,8 @@ class Database:
"""
if path is None:
path = find_spatialite()
if path is None:
raise OSError("Could not find SpatiaLite extension")
self.conn.enable_load_extension(True)
self.conn.load_extension(path)
@ -3005,7 +3009,7 @@ class Table(Queryable):
bar.update(1)
return jsonify_if_needed(fn(v))
fn_name = fn.__name__
fn_name = getattr(fn, "__name__", "fn")
if fn_name == "<lambda>":
fn_name = f"lambda_{abs(hash(fn))}"
self.db.register_function(convert_value, name=fn_name)
@ -3250,9 +3254,11 @@ class Table(Queryable):
)
# We can populate .last_pk right here
if num_records_processed == 1:
self.last_pk = tuple(record[pk] for pk in pks)
if len(self.last_pk) == 1:
self.last_pk = self.last_pk[0]
pk_values = tuple(record[pk] for pk in pks)
if len(pk_values) == 1:
self.last_pk = pk_values[0]
else:
self.last_pk = pk_values
return queries_and_params
def insert_chunk(