6969 RemoveSnapshotRefUpdate ,
7070 RemoveSnapshotsUpdate ,
7171 SetSnapshotRefUpdate ,
72- TableMetadata ,
7372 TableRequirement ,
7473 TableUpdate ,
7574 U ,
8786if 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
9490def _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+
854851class 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
0 commit comments