From 8dfbfa80b8282c14ed9732eff04ba07dbd5388e5 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 5 Jul 2026 15:36:46 -0700 Subject: [PATCH] Fix ty errors in transform_sql foreign key closures The fk_should_be_dropped and fk_with_renamed_columns closures captured the drop and rename parameters, but type checkers do not narrow captured variables inside nested functions so ty saw their Optional declared types. Bind the already-normalized values to fresh names for the closures to use. Co-Authored-By: Claude Fable 5 --- sqlite_utils/db.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index a739486..1c06b62 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -2439,6 +2439,11 @@ class Table(Queryable): create_table_foreign_keys.extend(foreign_keys) else: # Construct foreign_keys from current, plus add_foreign_keys, minus drop_foreign_keys + # Bind fresh names here - type checkers do not narrow captured + # variables inside nested functions, so the closures would + # otherwise see the Optional declared types of drop and rename + dropped_columns = drop + renamed_columns = rename def fk_should_be_dropped(fk: ForeignKey) -> bool: if drop_foreign_keys is not None: @@ -2451,10 +2456,12 @@ class Table(Queryable): # A tuple/list must match a compound key's columns exactly return True # Dropping any of a foreign key's columns drops the whole key - return any(column in drop for column in fk.columns) + return any(column in dropped_columns for column in fk.columns) def fk_with_renamed_columns(fk: ForeignKey) -> ForeignKey: - columns = tuple(rename.get(column) or column for column in fk.columns) + columns = tuple( + renamed_columns.get(column) or column for column in fk.columns + ) if fk.is_compound: return ForeignKey( self.name,