Skip to content

Commit 689310d

Browse files
committed
Fixed: Linting
`make lint` now passes
1 parent a08eb6b commit 689310d

3 files changed

Lines changed: 23 additions & 26 deletions

File tree

pyiceberg/table/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,7 @@
115115
update_table_metadata,
116116
)
117117
from pyiceberg.table.update.schema import UpdateSchema
118-
from pyiceberg.table.update.snapshot import (
119-
ManageSnapshots,
120-
UpdateSnapshot,
121-
_FastAppendFiles,
122-
ExpireSnapshots
123-
)
118+
from pyiceberg.table.update.snapshot import ExpireSnapshots, ManageSnapshots, UpdateSnapshot, _FastAppendFiles
124119
from pyiceberg.table.update.spec import UpdateSpec
125120
from pyiceberg.table.update.statistics import UpdateStatistics
126121
from pyiceberg.transforms import IdentityTransform
@@ -1079,7 +1074,7 @@ def manage_snapshots(self) -> ManageSnapshots:
10791074
ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B")
10801075
"""
10811076
return ManageSnapshots(transaction=Transaction(self, autocommit=True))
1082-
1077+
10831078
def expire_snapshots(self) -> ExpireSnapshots:
10841079
"""
10851080
Shorthand to run expire snapshots by id or by a timestamp.

pyiceberg/table/update/snapshot.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
RemoveSnapshotRefUpdate,
7070
RemoveSnapshotsUpdate,
7171
SetSnapshotRefUpdate,
72-
TableMetadata,
7372
TableRequirement,
7473
TableUpdate,
7574
U,
@@ -87,9 +86,6 @@
8786
if TYPE_CHECKING:
8887
from pyiceberg.table import Transaction
8988

90-
from pyiceberg.table.metadata import TableMetadata
91-
from pyiceberg.table.snapshots import Snapshot
92-
9389

9490
def _new_manifest_file_name(num: int, commit_uuid: uuid.UUID) -> str:
9591
return f"{commit_uuid}-m{num}.avro"
@@ -745,7 +741,7 @@ class ManageSnapshots(UpdateTableMetadata["ManageSnapshots"]):
745741
ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B")
746742
"""
747743

748-
_snapshot_ids_to_expire = set()
744+
_snapshot_ids_to_expire: Set[int] = set()
749745
_updates: Tuple[TableUpdate, ...] = ()
750746
_requirements: Tuple[TableRequirement, ...] = ()
751747

@@ -851,21 +847,24 @@ def remove_branch(self, branch_name: str) -> ManageSnapshots:
851847
"""
852848
return self._remove_ref_snapshot(ref_name=branch_name)
853849

850+
854851
class ExpireSnapshots(UpdateTableMetadata["ExpireSnapshots"]):
855852
"""
856853
Expire snapshots by ID.
854+
857855
Use table.expire_snapshots().<operation>().commit() to run a specific operation.
858856
Use table.expire_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations.
859857
Pending changes are applied on commit.
860858
"""
861-
862-
_snapshot_ids_to_expire = set()
859+
860+
_snapshot_ids_to_expire: Set[int] = set()
863861
_updates: Tuple[TableUpdate, ...] = ()
864862
_requirements: Tuple[TableRequirement, ...] = ()
865863

866864
def _commit(self) -> UpdatesAndRequirements:
867865
"""
868866
Commit the staged updates and requirements.
867+
869868
This will remove the snapshots with the given IDs.
870869
871870
Returns:
@@ -876,35 +875,37 @@ def _commit(self) -> UpdatesAndRequirements:
876875
self._updates += (update,)
877876
return self._updates, self._requirements
878877

879-
def _get_protected_snapshot_ids(self):
878+
def _get_protected_snapshot_ids(self) -> Set[int]:
880879
"""
881-
Get the IDs of protected snapshots. These are the HEAD snapshots of all branches
882-
and all tagged snapshots. These ids are to be excluded from expiration.
880+
Get the IDs of protected snapshots.
881+
882+
These are the HEAD snapshots of all branches and all tagged snapshots. These ids are to be excluded from expiration.
883+
883884
Returns:
884885
Set of protected snapshot IDs to exclude from expiration.
885886
"""
886-
protected_ids = set()
887-
887+
protected_ids: Set[int] = set()
888+
888889
for ref in self._transaction.table_metadata.refs.values():
889890
if ref.snapshot_ref_type in [SnapshotRefType.TAG, SnapshotRefType.BRANCH]:
890891
protected_ids.add(ref.snapshot_id)
891-
892+
892893
return protected_ids
893894

894895
def expire_snapshot_by_id(self, snapshot_id: int) -> ExpireSnapshots:
895896
"""
896897
Expire a snapshot by its ID.
897898
899+
This will mark the snapshot for expiration.
900+
898901
Args:
899902
snapshot_id (int): The ID of the snapshot to expire.
900-
901903
Returns:
902904
This for method chaining.
903905
"""
904-
905906
if self._transaction.table_metadata.snapshot_by_id(snapshot_id) is None:
906907
raise ValueError(f"Snapshot with ID {snapshot_id} does not exist.")
907-
908+
908909
if snapshot_id in self._get_protected_snapshot_ids():
909910
raise ValueError(f"Snapshot with ID {snapshot_id} is protected and cannot be expired.")
910911

tests/table/test_expire_snapshots.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ def test_expire_snapshot(table_v2: Table) -> None:
3131
uuid=uuid4(),
3232
)
3333

34-
# Mock the commit_table method to return the mock response
35-
table_v2.catalog.commit_table = MagicMock(return_value=mock_response)
34+
# Mock the catalog object and its commit_table method to return the mock response
35+
table_v2.catalog = MagicMock()
36+
table_v2.catalog.commit_table.return_value = mock_response
3637

3738
# Print snapshot IDs for debugging
3839
print(f"Snapshot IDs before expiration: {[snapshot.snapshot_id for snapshot in table_v2.metadata.snapshots]}")
@@ -46,7 +47,7 @@ def test_expire_snapshot(table_v2: Table) -> None:
4647
try:
4748
table_v2.expire_snapshots().expire_snapshot_by_id(EXPIRE_SNAPSHOT).commit()
4849
except Exception as e:
49-
assert False, f"Commit failed with error: {e}"
50+
raise AssertionError(f"Commit failed with error: {e}") from e
5051

5152
# Assert that commit_table was called once
5253
table_v2.catalog.commit_table.assert_called_once()

0 commit comments

Comments
 (0)