From 272def5e48e883ebfe7af090ef8340ea920075a1 Mon Sep 17 00:00:00 2001 From: Jonas Dedden Date: Tue, 29 Sep 2026 11:43:33 +0300 Subject: [PATCH] refactor: drop the CompactionOptions cast pylance 12 declares `CompactionOptions` as `TypedDict(total=False)`, so an empty dict and partial `CompactionOptions(...)` literals type-check without casting. Remove the cast in `compact_files` and the test-only `_compaction_options` helper. --- lance_ray/compaction.py | 10 ++-------- tests/test_distributed_compaction.py | 30 ++++++++++------------------ 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/lance_ray/compaction.py b/lance_ray/compaction.py index 485e7e23..e9970ad3 100644 --- a/lance_ray/compaction.py +++ b/lance_ray/compaction.py @@ -1,6 +1,6 @@ import logging from collections.abc import Callable -from typing import Any, Optional, cast +from typing import Any, Optional import lance from lance.lance import CompactionMetrics @@ -139,13 +139,7 @@ def compact_files( logger.info("Starting distributed compaction") # Step 1: Create the compaction plan - # Compaction.plan requires a dict; CompactionOptions is a TypedDict, so - # an empty instance stands in for "all defaults" when the caller omits it. - # It is declared with ``total=True`` upstream even though every key is - # optional at runtime, hence the cast instead of ``CompactionOptions()``. - compaction_plan = Compaction.plan( - dataset, compaction_options or cast(CompactionOptions, {}) - ) + compaction_plan = Compaction.plan(dataset, compaction_options or {}) logger.info(f"Compaction plan created with {compaction_plan.num_tasks()} tasks") diff --git a/tests/test_distributed_compaction.py b/tests/test_distributed_compaction.py index cd9b2cf9..c279b610 100644 --- a/tests/test_distributed_compaction.py +++ b/tests/test_distributed_compaction.py @@ -3,7 +3,6 @@ import tempfile from collections.abc import Iterator from pathlib import Path -from typing import Any, cast from unittest.mock import MagicMock, patch import lance @@ -22,15 +21,6 @@ def temp_dir() -> Iterator[str]: yield temp_dir -def _compaction_options(**options: Any) -> CompactionOptions: - """Build a partial ``CompactionOptions``. - - pylance declares the TypedDict as total even though every key is optional - at runtime, so a partial literal cannot be spelled out directly. - """ - return cast(CompactionOptions, options) - - def create_dataset_with_fragments( path: str | Path, fragment_data: list[pd.DataFrame] ) -> lance.LanceDataset: @@ -95,7 +85,7 @@ def test_basic_compaction(self, temp_dir: str) -> None: assert dataset.count_rows() == 20, "Should have 20 total rows" # Configure compaction to merge fragments (target 100 rows per fragment) - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -182,7 +172,7 @@ def test_deletion_compaction(self, temp_dir: str) -> None: assert dataset.count_rows() == 11, "Should have 11 rows after deletion" # Configure compaction to materialize deletions - compaction_options = _compaction_options( + compaction_options = CompactionOptions( materialize_deletions=True, materialize_deletions_threshold=0.5, # 50% threshold num_threads=1, @@ -230,7 +220,7 @@ def test_compaction_with_many_fragments(self, temp_dir: str) -> None: assert dataset.count_rows() == 5000, "Should have 5000 total rows" # Configure compaction to merge small fragments - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=20, num_threads=1, ) @@ -271,7 +261,7 @@ def test_compaction_no_work_needed(self, temp_dir: str) -> None: assert len(dataset.get_fragments()) == 1, "Should start with 1 fragment" # Configure compaction with target that matches current state - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -307,7 +297,7 @@ def test_compaction_with_ray_remote_args(self, temp_dir: str) -> None: create_dataset_with_fragments(dataset_path, [fragment1, fragment2]) # Configure compaction - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -346,7 +336,7 @@ def test_compaction_with_storage_options(self, temp_dir: str) -> None: create_dataset_with_fragments(dataset_path, [fragment1, fragment2]) # Configure compaction - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -385,7 +375,7 @@ def test_compaction_auto_adjust_workers(self, temp_dir: str) -> None: create_dataset_with_fragments(dataset_path, [fragment1, fragment2]) # Configure compaction - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -430,7 +420,7 @@ def test_compaction_preserves_data(self, temp_dir: str) -> None: ) # Configure and execute compaction - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -503,7 +493,7 @@ def test_compaction_with_directory_namespace(self, temp_dir: str) -> None: assert len(dataset.get_fragments()) == 2, "Should start with 2 fragments" assert dataset.count_rows() == 20, "Should have 20 total rows" - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, ) @@ -581,7 +571,7 @@ def test_compact_database_two_tables_both_compacted(self, temp_dir: str) -> None database = ["compact_db"] table_names = ["table_a", "table_b"] - compaction_options = _compaction_options( + compaction_options = CompactionOptions( target_rows_per_fragment=100, num_threads=1, )