Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): enable strict typing for migration tests (#43347)
kr1shna-exe63094c91ba5e
diff --git a/api/tests/unit_tests/commands/test_legacy_model_type_migration.py b/api/tests/unit_tests/commands/test_legacy_model_type_migration.pyindex ffdfd30c8db..e9dc551ad8a 100644--- a/api/tests/unit_tests/commands/test_legacy_model_type_migration.py+++ b/api/tests/unit_tests/commands/test_legacy_model_type_migration.py@@ -6,15 +6,16 @@ import jsonimport osimport threadingimport time-from collections.abc import Iterator+from collections.abc import Iterator, Sequencefrom datetime import datetime, timedeltafrom pathlib import Path-from types import SimpleNamespace-from typing import cast+from types import ModuleType, SimpleNamespace+from typing import cast, overrideimport pytestimport sqlalchemy as safrom click.testing import CliRunner+from sqlalchemy import eventfrom sqlalchemy.exc import OperationalErrorfrom sqlalchemy.orm import Session, SessionTransaction, sessionmaker@@ -23,10 +24,13 @@ from models import Dataset, DatasetPermission, DatasetPermissionEnum, TenantAccofrom models.account import Tenantfrom models.base import TypeBasefrom models.enums import CredentialSourceType-from models.provider import ProviderModel+from models.provider import LoadBalancingModelConfig, ProviderModel+from services import legacy_model_type_migration as migration_module+from services.enterprise.rbac_service import ReplaceMemberBindings, ReplaceUserAccessPoliciesfrom tests.helpers.legacy_model_type_migration import (ALL_TABLE_NAMES,LEGACY_TO_CANONICAL,+ DirtyDataFixture,assert_tenant_rows_use_only_canonical_model_types,count_rows,create_minimal_legacy_model_type_schema,@@ -36,24 +40,27 @@ from tests.helpers.legacy_model_type_migration import ()-def dirty_fixture(sqlite_engine: sa.Engine):- return seed_legacy_model_type_dirty_data(sqlite_engine)+class _DatabaseError(Exception):+ def __init__(+ self,+ *,+ pgcode: str | None = None,+ sqlstate: str | None = None,+ errno: int | None = None,+ ) -> None:+ super().__init__()+ self.pgcode = pgcode+ self.sqlstate = sqlstate+ self.errno = errno@pytest.fixture-def migration_module():- try:- return importlib.import_module("services.legacy_model_type_migration")- except ModuleNotFoundError as exc: # pragma: no cover - explicit TDD failure path- pytest.fail(- "services.legacy_model_type_migration is missing. "- "Implement LegacyModelTypeMigrationService before running these tests."- )+def dirty_fixture(sqlite_engine: sa.Engine) -> DirtyDataFixture:+ return seed_legacy_model_type_dirty_data(sqlite_engine)@pytest.fixture-def command_module():+def command_module() -> ModuleType:try:return importlib.import_module("commands.data_migrate")except ModuleNotFoundError as exc: # pragma: no cover - explicit TDD failure path@@ -69,7 +76,9 @@ def rbac_session(sqlite_engine: sa.Engine, monkeypatch: pytest.MonkeyPatch) -> ITypeBase.metadata.create_all(sqlite_engine,- tables=[Dataset.__table__, DatasetPermission.__table__, TenantAccountJoin.__table__],+ tables=[+ TypeBase.metadata.tables[model.__tablename__] for model in (Dataset, DatasetPermission, TenantAccountJoin)+ ],)factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)monkeypatch.setattr("commands.rbac.session_factory.create_session", factory)@@ -174,14 +183,21 @@ def _cache_event_row_ids(def _patch_batch_size(monkeypatch: pytest.MonkeyPatch,- migration_module,*,batch_size: int,) -> None:original_init = migration_module.Migration.__init__- def _patched_init(self, *args, **kwargs) -> None:- original_init(self, *args, **kwargs)+ def _patched_init(+ self: migration_module.Migration,+ tenant_id: str,+ engine: sa.Engine,+ apply: bool,+ output: io.TextIOBase,+ model_types: Sequence[ModelType],+ orm_models: Sequence[migration_module.ORMModel],+ ) -> None:+ original_init(self, tenant_id, engine, apply, output, model_types, orm_models)self._batch_size = batch_sizemonkeypatch.setattr(migration_module.Migration, "__init__", _patched_init)@@ -232,7 +248,7 @@ def _insert_provider_model(def _insert_tenant(engine: sa.Engine, *, tenant_id: str) -> None:with engine.begin() as conn:conn.execute(- Tenant.__table__.insert().values(+ sa.insert(Tenant).values(id=tenant_id,name=f"Tenant {tenant_id}",plan="basic",@@ -367,20 +383,20 @@ def _insert_load_balancing_model_config()-def test_data_migrate_group_registers_dataset_permission_rbac_migration(command_module) -> None:+def test_data_migrate_group_registers_dataset_permission_rbac_migration(command_module: ModuleType) -> None:command = command_module.data_migrate.commands["rbac-migrate-dataset-permissions"]assert command is command_module.migrate_dataset_permissions_to_rbacassert "operator_account_id" not in {param.name for param in command.params}-def test_data_migrate_group_registers_resource_whitelist_scope_migration(command_module) -> None:+def test_data_migrate_group_registers_resource_whitelist_scope_migration(command_module: ModuleType) -> None:command = command_module.data_migrate.commands["rbac-migrate-resource-whitelist-scopes"]assert command is command_module.migrate_only_me_resource_whitelist_scopes_to_automatic_include-def test_dataset_permission_rbac_migration_help_mentions_binding_clear_side_effect(command_module) -> None:+def test_dataset_permission_rbac_migration_help_mentions_binding_clear_side_effect(command_module: ModuleType) -> None:result = CliRunner().invoke(command_module.data_migrate,["rbac-migrate-dataset-permissions", "--help"],@@ -408,7 +424,7 @@ def test_dataset_permission_rbac_migration_maps_legacy_permissions_to_enum_scopedef test_dataset_permission_rbac_migration_uses_dataset_creator_as_operator(- command_module,+ command_module: ModuleType,rbac_session: Session,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -417,8 +433,8 @@ def test_dataset_permission_rbac_migration_uses_dataset_creator_as_operator(calls: list[dict[str, object]] = []read_transaction_ended = False- def fake_replace_whitelist(**kwargs):- assert read_transaction_ended is True+ def fake_replace_whitelist(**kwargs: object) -> None:+ assert read_transaction_endedcalls.append(kwargs)def _record_transaction_end(session: Session, transaction: object) -> None:@@ -427,7 +443,7 @@ def test_dataset_permission_rbac_migration_uses_dataset_creator_as_operator(if session.get_bind() is rbac_session.get_bind():read_transaction_ended = True- sa.event.listen(Session, "after_transaction_end", _record_transaction_end)+ event.listen(Session, "after_transaction_end", _record_transaction_end)monkeypatch.setattr(rbac_module.RBACService.DatasetAccess, "replace_whitelist", fake_replace_whitelist)try:command_module.migrate_dataset_permissions_to_rbac.callback(@@ -437,16 +453,18 @@ def test_dataset_permission_rbac_migration_uses_dataset_creator_as_operator(dry_run=False,)finally:- sa.event.remove(Session, "after_transaction_end", _record_transaction_end)+ event.remove(Session, "after_transaction_end", _record_transaction_end)assert calls[0]["tenant_id"] == "tenant-1"assert calls[0]["account_id"] == "creator-account-1"assert calls[0]["dataset_id"] == "dataset-1"- assert calls[0]["payload"].automatic_include_workspace_members is False+ payload = calls[0]["payload"]+ assert isinstance(payload, ReplaceMemberBindings)+ assert payload.automatic_include_workspace_members is Falsedef test_dataset_permission_rbac_migration_dry_run_outputs_structured_proposed_changes(- command_module,+ command_module: ModuleType,rbac_session: Session,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -499,7 +517,7 @@ def test_dataset_permission_rbac_migration_dry_run_outputs_structured_proposed_cdef test_resource_whitelist_scope_migration_specific_preserves_existing_members(- command_module,+ command_module: ModuleType,rbac_session: Session,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -537,13 +555,17 @@ def test_resource_whitelist_scope_migration_specific_preserves_existing_members(dry_run=False,)- assert replace_whitelist_calls[0]["payload"].automatic_include_workspace_members is False- assert replace_policy_calls[0]["payload"].access_policy_ids == ["default"]- assert replace_policy_calls[0]["payload"].account_ids == ["member-account-1", "member-account-2"]+ whitelist_payload = replace_whitelist_calls[0]["payload"]+ policy_payload = replace_policy_calls[0]["payload"]+ assert isinstance(whitelist_payload, ReplaceMemberBindings)+ assert isinstance(policy_payload, ReplaceUserAccessPolicies)+ assert whitelist_payload.automatic_include_workspace_members is False+ assert policy_payload.access_policy_ids == ["default"]+ assert policy_payload.account_ids == ["member-account-1", "member-account-2"]def test_resource_whitelist_scope_migration_all_syncs_workspace_members(- command_module,+ command_module: ModuleType,rbac_session: Session,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -593,13 +615,17 @@ def test_resource_whitelist_scope_migration_all_syncs_workspace_members(dry_run=False,)- assert replace_whitelist_calls[0]["payload"].automatic_include_workspace_members is True- assert replace_policy_calls[0]["payload"].access_policy_ids == ["default"]- assert set(replace_policy_calls[0]["payload"].account_ids) == {"maintainer-account-1", "member-account-1"}+ whitelist_payload = replace_whitelist_calls[0]["payload"]+ policy_payload = replace_policy_calls[0]["payload"]+ assert isinstance(whitelist_payload, ReplaceMemberBindings)+ assert isinstance(policy_payload, ReplaceUserAccessPolicies)+ assert whitelist_payload.automatic_include_workspace_members is True+ assert policy_payload.access_policy_ids == ["default"]+ assert set(policy_payload.account_ids) == {"maintainer-account-1", "member-account-1"}def test_only_me_resource_whitelist_scope_migration_syncs_workspace_members(- command_module,+ command_module: ModuleType,rbac_session: Session,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -649,13 +675,17 @@ def test_only_me_resource_whitelist_scope_migration_syncs_workspace_members(dry_run=False,)- assert replace_whitelist_calls[0]["payload"].automatic_include_workspace_members is True- assert replace_policy_calls[0]["payload"].access_policy_ids == ["default"]- assert set(replace_policy_calls[0]["payload"].account_ids) == {"maintainer-account-1", "member-account-1"}+ whitelist_payload = replace_whitelist_calls[0]["payload"]+ policy_payload = replace_policy_calls[0]["payload"]+ assert isinstance(whitelist_payload, ReplaceMemberBindings)+ assert isinstance(policy_payload, ReplaceUserAccessPolicies)+ assert whitelist_payload.automatic_include_workspace_members is True+ assert policy_payload.access_policy_ids == ["default"]+ assert set(policy_payload.account_ids) == {"maintainer-account-1", "member-account-1"}def test_data_migrate_command_defaults_output_to_stdout_stream(- command_module,+ command_module: ModuleType,monkeypatch: pytest.MonkeyPatch,tmp_path: Path,) -> None:@@ -696,7 +726,8 @@ def test_data_migrate_command_defaults_output_to_stdout_stream(tenant_id_file.write_text("tenant-alpha\n", encoding="utf-8")data_migrate = command_module.data_migrate- legacy_model_types = cast(object, data_migrate.commands["legacy-model-types"])+ legacy_model_types = data_migrate.commands["legacy-model-types"]+ assert legacy_model_types.callback is not Nonelegacy_model_types.callback(apply=True,@@ -717,7 +748,7 @@ def test_data_migrate_command_defaults_output_to_stdout_stream(def test_data_migrate_command_opens_output_file_and_closes_stream(- command_module,+ command_module: ModuleType,monkeypatch: pytest.MonkeyPatch,tmp_path: Path,) -> None:@@ -757,7 +788,8 @@ def test_data_migrate_command_opens_output_file_and_closes_stream(output_path = tmp_path / "migration.jsonl"data_migrate = command_module.data_migrate- legacy_model_types = cast(object, data_migrate.commands["legacy-model-types"])+ legacy_model_types = data_migrate.commands["legacy-model-types"]+ assert legacy_model_types.callback is not Nonelegacy_model_types.callback(apply=False,@@ -771,7 +803,7 @@ def test_data_migrate_command_opens_output_file_and_closes_stream(output_stream = cast(io.TextIOBase, service_calls[0]["output"])assert service_calls[0]["concurrency"] == 3assert output_stream is not output_path- assert isinstance(output_stream, io.TextIOBase)+ assert isinstance(output_stream, io.TextIOWrapper)assert Path(output_stream.name) == output_pathassert output_stream.closed is Trueassert output_path.read_text(encoding="utf-8") == '{"event":"test"}\n'@@ -838,7 +870,6 @@ def test_data_migrate_command_defaults_concurrency_from_cpu_count_or_falls_back_def test_service_migrate_batches_by_tenant_respects_selected_tables_without_reverse_dependency_expansion(- migration_module,sqlite_engine: sa.Engine,) -> None:seen_runs: list[tuple[str, tuple[str, ...], tuple[ModelType, ...]]] = []@@ -852,11 +883,11 @@ def test_service_migrate_batches_by_tenant_respects_selected_tables_without_reveapply: bool,output: io.TextIOBase,model_types: tuple[ModelType, ...],- orm_models: tuple[type[object], ...],+ orm_models: tuple[migration_module.ORMModel, ...],) -> None:assert engine is sqlite_engineassert apply is False- seen_runs.append((tenant_id, tuple(model.__table__.name for model in orm_models), model_types))+ seen_runs.append((tenant_id, tuple(model.__tablename__ for model in orm_models), model_types))def run(self) -> None:return None@@ -884,7 +915,6 @@ def test_service_migrate_batches_by_tenant_respects_selected_tables_without_revedef test_service_migrate_without_tenant_ids_discovers_tenants_per_selected_table_without_querying_tenants(- migration_module,sqlite_engine: sa.Engine,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -931,11 +961,11 @@ def test_service_migrate_without_tenant_ids_discovers_tenants_per_selected_tableapply: bool,output: io.TextIOBase,model_types: tuple[ModelType, ...],- orm_models: tuple[type[object], ...],+ orm_models: tuple[migration_module.ORMModel, ...],) -> None:assert engine is sqlite_engineassert apply is False- seen_runs.append((tenant_id, tuple(model.__table__.name for model in orm_models), model_types))+ seen_runs.append((tenant_id, tuple(model.__tablename__ for model in orm_models), model_types))def run(self) -> None:return None@@ -951,7 +981,7 @@ def test_service_migrate_without_tenant_ids_discovers_tenants_per_selected_tabledel conn, cursor, parameters, context, executemanyexecuted_sql.append(statement)- sa.event.listen(sqlite_engine, "before_cursor_execute", _record_sql)+ event.listen(sqlite_engine, "before_cursor_execute", _record_sql)try:monkeypatch.setattr(migration_module, "Migration", FakeMigration)service = migration_module.LegacyModelTypeMigrationService(@@ -963,7 +993,7 @@ def test_service_migrate_without_tenant_ids_discovers_tenants_per_selected_tableservice.migrate()finally:- sa.event.remove(sqlite_engine, "before_cursor_execute", _record_sql)+ event.remove(sqlite_engine, "before_cursor_execute", _record_sql)assert seen_runs == [(provider_tenant_id, ("provider_models",), (ModelType.LLM,)),@@ -987,7 +1017,6 @@ def test_service_migrate_without_tenant_ids_discovers_tenants_per_selected_tabledef test_service_migrate_without_tenant_ids_filters_provider_model_tenants_by_selected_model_types(- migration_module,sqlite_engine: sa.Engine,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -1034,11 +1063,11 @@ def test_service_migrate_without_tenant_ids_filters_provider_model_tenants_by_seapply: bool,output: io.TextIOBase,model_types: tuple[ModelType, ...],- orm_models: tuple[type[object], ...],+ orm_models: tuple[migration_module.ORMModel, ...],) -> None:assert engine is sqlite_engineassert apply is False- seen_runs.append((tenant_id, tuple(model.__table__.name for model in orm_models), model_types))+ seen_runs.append((tenant_id, tuple(model.__tablename__ for model in orm_models), model_types))def run(self) -> None:return None@@ -1059,7 +1088,6 @@ def test_service_migrate_without_tenant_ids_filters_provider_model_tenants_by_sedef test_service_migrate_without_tenant_ids_discovers_all_load_balancing_tenants_for_simpler_table_scoped_query(- migration_module,sqlite_engine: sa.Engine,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -1112,11 +1140,11 @@ def test_service_migrate_without_tenant_ids_discovers_all_load_balancing_tenantsapply: bool,output: io.TextIOBase,model_types: tuple[ModelType, ...],- orm_models: tuple[type[object], ...],+ orm_models: tuple[migration_module.ORMModel, ...],) -> None:assert engine is sqlite_engineassert apply is False- seen_runs.append((tenant_id, tuple(model.__table__.name for model in orm_models), model_types))+ seen_runs.append((tenant_id, tuple(model.__tablename__ for model in orm_models), model_types))def run(self) -> None:return None@@ -1141,7 +1169,6 @@ def test_service_migrate_without_tenant_ids_discovers_all_load_balancing_tenantsdef test_service_migrate_with_concurrency_greater_than_one_runs_tenants_in_parallel_without_changing_migration_scope(- migration_module,sqlite_engine: sa.Engine,) -> None:init_calls: list[dict[str, object]] = []@@ -1162,7 +1189,7 @@ def test_service_migrate_with_concurrency_greater_than_one_runs_tenants_in_paralapply: bool,output: io.TextIOBase,model_types: tuple[ModelType, ...],- orm_models: tuple[type[object], ...],+ orm_models: tuple[migration_module.ORMModel, ...],) -> None:self._tenant_id = tenant_idinit_calls.append(@@ -1171,7 +1198,7 @@ def test_service_migrate_with_concurrency_greater_than_one_runs_tenants_in_paral"engine": engine,"apply": apply,"model_types": model_types,- "table_names": tuple(model.__table__.name for model in orm_models),+ "table_names": tuple(model.__tablename__ for model in orm_models),})@@ -1226,7 +1253,6 @@ def test_service_migrate_with_concurrency_greater_than_one_runs_tenants_in_paraldef test_service_parallel_migrate_serializes_shared_output_by_line(- migration_module,sqlite_engine: sa.Engine,) -> None:worker_errors: list[BaseException] = []@@ -1239,6 +1265,7 @@ def test_service_parallel_migrate_serializes_shared_output_by_line(self._in_write = Falseself._state_lock = threading.Lock()+ @overridedef write(self, s: str) -> int:with self._state_lock:if self._in_write:@@ -1260,7 +1287,7 @@ def test_service_parallel_migrate_serializes_shared_output_by_line(apply: bool,output: io.TextIOBase,model_types: tuple[ModelType, ...],- orm_models: tuple[type[object], ...],+ orm_models: tuple[migration_module.ORMModel, ...],) -> None:self._tenant_id = tenant_idself._output = output@@ -1316,15 +1343,14 @@ def test_service_parallel_migrate_serializes_shared_output_by_line(def test_migration_dry_run_emits_json_lines_without_db_or_cache_mutation(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:before = snapshot_legacy_model_type_state(sqlite_engine)deleted_cache_keys: list[str] = []- def _record_delete(self) -> None:+ def _record_delete(self: migration_module.ProviderCredentialsCache) -> None:deleted_cache_keys.append(self.cache_key)monkeypatch.setattr(migration_module.ProviderCredentialsCache, "delete", _record_delete)@@ -1353,15 +1379,14 @@ def test_migration_dry_run_emits_json_lines_without_db_or_cache_mutation(def test_dry_run_and_apply_share_processing_scope_and_differ_only_on_side_effects(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:before = snapshot_legacy_model_type_state(sqlite_engine)deleted_cache_keys: list[str] = []- def _record_delete(self) -> None:+ def _record_delete(self: migration_module.ProviderCredentialsCache) -> None:deleted_cache_keys.append(self.cache_key)monkeypatch.setattr(migration_module.ProviderCredentialsCache, "delete", _record_delete)@@ -1430,9 +1455,8 @@ def test_dry_run_and_apply_share_processing_scope_and_differ_only_on_side_effectdef test_provider_models_processing_uses_same_plan_locking_and_transaction_entry_for_dry_run_and_apply(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:dry_migration = migration_module.Migration(@@ -1469,32 +1493,45 @@ def test_provider_models_processing_uses_same_plan_locking_and_transaction_entryif session.get_bind() is sqlite_engine and transaction.parent is None:begin_calls.append(current_phase["name"])- def _fake_build_plan(self, session: Session, candidate, *, lock_rows: bool):+ def _fake_build_plan(+ self: migration_module.Migration,+ session: Session,+ candidate: migration_module._RowWithRawModelType[ProviderModel],+ *,+ lock_rows: bool,+ ) -> migration_module._ProviderModelGroupPlan:assert session.get_bind() is sqlite_enginelock_rows_seen.append((current_phase["name"], lock_rows))return migration_module._ProviderModelGroupPlan(- group_row_ids=[str(candidate.row.id)],+ group_row_ids=[candidate.row.id],winner=None,loser_rows=[],)- def _fake_emit_plan(self, plan, *, session, tx_id: str, business_key: dict[str, object]) -> None:+ def _fake_emit_plan(+ self: migration_module.Migration,+ plan: migration_module._ProviderModelGroupPlan,+ *,+ session: Session,+ tx_id: str,+ business_key: migration_module._BusinessKey,+ ) -> None:return None- def _fake_configure(self, session: Session) -> None:+ def _fake_configure(self: migration_module.Migration, session: Session) -> None:assert session.get_bind() is sqlite_engineconfigure_calls.append(current_phase["name"])monkeypatch.setattr(migration_module.Migration, "_build_provider_model_group_plan", _fake_build_plan)monkeypatch.setattr(migration_module.Migration, "_emit_provider_model_group_plan", _fake_emit_plan)monkeypatch.setattr(migration_module.Migration, "_configure_lock_timeout", _fake_configure)- sa.event.listen(Session, "after_transaction_create", _record_begin)+ event.listen(Session, "after_transaction_create", _record_begin)try:dry_migration._process_provider_model_group(candidate, business_key)current_phase["name"] = "apply"apply_migration._process_provider_model_group(candidate, business_key)finally:- sa.event.remove(Session, "after_transaction_create", _record_begin)+ event.remove(Session, "after_transaction_create", _record_begin)assert [phase for phase, _ in lock_rows_seen] == ["dry", "apply"]assert lock_rows_seen[0][1] == lock_rows_seen[1][1]@@ -1505,18 +1542,17 @@ def test_provider_models_processing_uses_same_plan_locking_and_transaction_entry@pytest.mark.parametrize(("orig", "expected"),[- (SimpleNamespace(pgcode="55P03"), True),- (SimpleNamespace(sqlstate="55P03"), True),- (SimpleNamespace(errno=1205), True),+ (_DatabaseError(pgcode="55P03"), True),+ (_DatabaseError(sqlstate="55P03"), True),+ (_DatabaseError(errno=1205), True),(RuntimeError("canceling statement due to lock timeout"), True),- (SimpleNamespace(pgcode="23505"), False),- (SimpleNamespace(errno=1213), False),+ (_DatabaseError(pgcode="23505"), False),+ (_DatabaseError(errno=1213), False),],)def test_is_lock_timeout_error_prefers_structured_backend_codes(- migration_module,sqlite_engine: sa.Engine,- orig: object,+ orig: BaseException,expected: bool,) -> None:migration = migration_module.Migration(@@ -1533,7 +1569,6 @@ def test_is_lock_timeout_error_prefers_structured_backend_codes(def test_process_load_balancing_model_config_row_logs_stacktrace_for_lock_timeout(- migration_module,sqlite_engine: sa.Engine,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -1563,7 +1598,7 @@ def test_process_load_balancing_model_config_row_logs_stacktrace_for_lock_timeouorm_models=(migration_module.LoadBalancingModelConfig,),)candidate = migration._load_load_balancing_model_config_candidates(None)[0]- lock_timeout_exc = OperationalError("SELECT 1", {}, SimpleNamespace(pgcode="55P03"))+ lock_timeout_exc = OperationalError("SELECT 1", {}, _DatabaseError(pgcode="55P03"))transaction_begins = 0def _record_begin(session: Session, transaction: SessionTransaction) -> None:@@ -1571,7 +1606,13 @@ def test_process_load_balancing_model_config_row_logs_stacktrace_for_lock_timeouif session.get_bind() is sqlite_engine and transaction.parent is None:transaction_begins += 1- def _fake_reload(self, session, original_candidate, *, lock_rows: bool):+ def _fake_reload(+ self: migration_module.Migration,+ session: Session,+ original_candidate: migration_module._RowWithRawModelType[LoadBalancingModelConfig],+ *,+ lock_rows: bool,+ ) -> migration_module._RowWithRawModelType[LoadBalancingModelConfig] | None:raise lock_timeout_excmonkeypatch.setattr(migration_module.Migration, "_configure_lock_timeout", lambda self, session: None)@@ -1581,18 +1622,18 @@ def test_process_load_balancing_model_config_row_logs_stacktrace_for_lock_timeou_fake_reload,)- sa.event.listen(Session, "after_transaction_create", _record_begin)+ event.listen(Session, "after_transaction_create", _record_begin)try:migration._process_load_balancing_model_config_row(candidate)finally:- sa.event.remove(Session, "after_transaction_create", _record_begin)+ event.remove(Session, "after_transaction_create", _record_begin)lines = _parse_json_lines(output)assert len(lines) == 1assert lines[0]["event"] == "lock_timeout_skipped"attrs = cast(dict[str, object], lines[0]["attrs"])assert attrs["table_name"] == "load_balancing_model_configs"- assert attrs["id"] == str(candidate.row.id)+ assert attrs["id"] == candidate.row.idassert attrs["error"] == str(lock_timeout_exc)assert isinstance(attrs["stacktrace"], str)assert "OperationalError" in attrs["stacktrace"]@@ -1600,7 +1641,6 @@ def test_process_load_balancing_model_config_row_logs_stacktrace_for_lock_timeoudef test_process_load_balancing_model_config_row_logs_update_after_sql_execution(- migration_module,sqlite_engine: sa.Engine,monkeypatch: pytest.MonkeyPatch,) -> None:@@ -1648,19 +1688,25 @@ def test_process_load_balancing_model_config_row_logs_update_after_sql_executionif statement.lstrip().upper().startswith("UPDATE"):action_log.append("sql_execute")- def _fake_configure(self, session) -> None:+ def _fake_configure(self: migration_module.Migration, session: Session) -> None:action_log.append("configure_lock_timeout")original_reload = migration_module.Migration._reload_load_balancing_model_config_candidate- def _record_reload(self, session: Session, original_candidate, *, lock_rows: bool):+ def _record_reload(+ self: migration_module.Migration,+ session: Session,+ original_candidate: migration_module._RowWithRawModelType[LoadBalancingModelConfig],+ *,+ lock_rows: bool,+ ) -> migration_module._RowWithRawModelType[LoadBalancingModelConfig] | None:action_log.append(f"reload_candidate:{lock_rows}")return original_reload(self, session, original_candidate, lock_rows=lock_rows)- def _fake_log_row_updated(self, *args, **kwargs) -> None:+ def _fake_log_row_updated(self: migration_module.Migration, *args: object, **kwargs: object) -> None:action_log.append("log_row_updated")- def _fake_cache_cleanup(self, *, row_id: str, tx_id: str) -> None:+ def _fake_cache_cleanup(self: migration_module.Migration, *, row_id: str, tx_id: str) -> None:action_log.append("cache_cleanup")monkeypatch.setattr(migration_module.Migration, "_configure_lock_timeout", _fake_configure)@@ -1676,13 +1722,13 @@ def test_process_load_balancing_model_config_row_logs_update_after_sql_execution_fake_cache_cleanup,)- sa.event.listen(Session, "after_transaction_create", _record_begin)- sa.event.listen(sqlite_engine, "before_cursor_execute", _record_sql)+ event.listen(Session, "after_transaction_create", _record_begin)+ event.listen(sqlite_engine, "before_cursor_execute", _record_sql)try:migration._process_load_balancing_model_config_row(candidate)finally:- sa.event.remove(sqlite_engine, "before_cursor_execute", _record_sql)- sa.event.remove(Session, "after_transaction_create", _record_begin)+ event.remove(sqlite_engine, "before_cursor_execute", _record_sql)+ event.remove(Session, "after_transaction_create", _record_begin)assert action_log == ["begin",@@ -1699,12 +1745,11 @@ def test_process_load_balancing_model_config_row_logs_update_after_sql_executiondef test_load_balancing_model_config_cache_delete_failure_logs_stacktrace(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:- def _raise_delete_failure(self) -> None:+ def _raise_delete_failure(self: migration_module.ProviderCredentialsCache) -> None:raise RuntimeError("cache delete boom")monkeypatch.setattr(migration_module.ProviderCredentialsCache, "delete", _raise_delete_failure)@@ -1730,13 +1775,12 @@ def test_load_balancing_model_config_cache_delete_failure_logs_stacktrace(assert len(failed_events) == 1assert failed_events[0]["error"] == "cache delete boom"assert isinstance(failed_events[0]["stacktrace"], str)- assert "RuntimeError: cache delete boom" in cast(str, failed_events[0]["stacktrace"])+ assert "RuntimeError: cache delete boom" in failed_events[0]["stacktrace"]def test_group_completed_logs_exist_for_all_grouped_tables_and_use_canonical_model_type(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,) -> None:output = io.StringIO()@@ -1781,12 +1825,11 @@ def test_group_completed_logs_exist_for_all_grouped_tables_and_use_canonical_moddef test_provider_models_group_completed_log_includes_related_canonical_row_ids(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:- _patch_batch_size(monkeypatch, migration_module, batch_size=1)+ _patch_batch_size(monkeypatch, batch_size=1)inserted_row_id = "00000000-0000-0000-0000-00000000aa01"created_at = datetime(2025, 1, 1, 10, 0, 0)updated_at = created_at + timedelta(minutes=5)@@ -1839,12 +1882,11 @@ def test_provider_models_group_completed_log_includes_related_canonical_row_ids(def test_provider_model_settings_group_crossing_batches_is_completed_once_with_all_group_row_ids(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:- _patch_batch_size(monkeypatch, migration_module, batch_size=1)+ _patch_batch_size(monkeypatch, batch_size=1)inserted_row_id = "00000000-0000-0000-0000-00000000cc01"created_at = datetime(2025, 1, 1, 9, 0, 0)updated_at = created_at + timedelta(minutes=10)@@ -1898,9 +1940,8 @@ def test_provider_model_settings_group_crossing_batches_is_completed_once_with_adef test_load_balancing_inherit_rows_are_deduplicated_by_normalized_model_type_before_canonicalization(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:older_canonical_row_id = "00000000-0000-0000-0000-00000000dd01"@@ -1939,7 +1980,7 @@ def test_load_balancing_inherit_rows_are_deduplicated_by_normalized_model_type_bdeleted_cache_keys: list[str] = []- def _record_delete(self) -> None:+ def _record_delete(self: migration_module.ProviderCredentialsCache) -> None:deleted_cache_keys.append(self.cache_key)monkeypatch.setattr(migration_module.ProviderCredentialsCache, "delete", _record_delete)@@ -2086,9 +2127,8 @@ def test_load_balancing_inherit_rows_are_deduplicated_by_normalized_model_type_bdef test_load_balancing_non_inherit_rows_do_not_participate_in_normalized_model_type_deduplication(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,) -> None:inserted_row_id = "00000000-0000-0000-0000-00000000dd03"created_at = datetime(2025, 1, 1, 8, 0, 0)@@ -2141,14 +2181,13 @@ def test_load_balancing_non_inherit_rows_do_not_participate_in_normalized_model_def test_migration_apply_updates_all_five_tables_and_rewrites_credential_references(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,monkeypatch: pytest.MonkeyPatch,) -> None:deleted_cache_keys: list[str] = []- def _record_delete(self) -> None:+ def _record_delete(self: migration_module.ProviderCredentialsCache) -> None:deleted_cache_keys.append(self.cache_key)monkeypatch.setattr(migration_module.ProviderCredentialsCache, "delete", _record_delete)@@ -2207,9 +2246,8 @@ def test_migration_apply_updates_all_five_tables_and_rewrites_credential_referendef test_migration_filters_by_tenant_model_types_and_tables(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,) -> None:before_primary_credentials = fetch_table_rows(sqlite_engine,@@ -2296,9 +2334,8 @@ def test_migration_filters_by_tenant_model_types_and_tables(def test_migration_does_not_merge_credentials_with_different_credential_name(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,) -> None:service = migration_module.LegacyModelTypeMigrationService(engine=sqlite_engine,@@ -2328,9 +2365,8 @@ def test_migration_does_not_merge_credentials_with_different_credential_name(def test_migration_is_idempotent_on_second_apply(- migration_module,sqlite_engine: sa.Engine,- dirty_fixture,+ dirty_fixture: DirtyDataFixture,) -> None:service = migration_module.LegacyModelTypeMigrationService(engine=sqlite_engine,diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.tomlindex 87b2c282983..eb978f54d8a 100644--- a/api/tests/unit_tests/pyrefly.toml+++ b/api/tests/unit_tests/pyrefly.toml@@ -21,7 +21,6 @@ project-excludes = ["commands/test_fix_app_site_missing.py","commands/test_generate_swagger_markdown_docs.py","commands/test_generate_swagger_specs.py",- "commands/test_legacy_model_type_migration.py","commands/test_lint_response_contracts.py","commands/test_reset_encrypt_key_pair.py","commands/test_upgrade_db.py",