@@ -169,9 +169,7 @@ def __init__(
169169 TableProperties .WRITE_AVRO_COMPRESSION , TableProperties .WRITE_AVRO_COMPRESSION_DEFAULT
170170 )
171171 self ._target_branch = self ._validate_target_branch (branch = branch )
172- self ._parent_snapshot_id = (
173- snapshot .snapshot_id if (snapshot := self ._transaction .table_metadata .snapshot_by_name (self ._target_branch )) else None
174- )
172+ self ._parent_snapshot_id = self ._current_branch_head_id ()
175173 self ._starting_snapshot_id = self ._parent_snapshot_id
176174 self ._predicate = AlwaysFalse ()
177175 self ._case_sensitive = True
@@ -187,6 +185,11 @@ def _validate_target_branch(self, branch: str | None) -> str | None:
187185 raise ValueError (f"{ branch } is a tag, not a branch. Tags cannot be targets for producing snapshots" )
188186 return branch
189187
188+ def _current_branch_head_id (self ) -> int | None :
189+ """Return the snapshot id at the head of the target branch, or None if the branch has no snapshot."""
190+ snapshot = self ._transaction .table_metadata .snapshot_by_name (self ._target_branch )
191+ return snapshot .snapshot_id if snapshot else None
192+
190193 def append_data_file (self , data_file : DataFile ) -> _SnapshotProducer [U ]:
191194 self ._added_data_files .append (data_file )
192195 return self
@@ -444,9 +447,7 @@ def _refresh_for_retry(self) -> None:
444447 """Reset state for a retry attempt with refreshed metadata."""
445448 self ._uncommitted_manifests .extend (self ._written_manifests )
446449 self ._written_manifests .clear ()
447- self ._parent_snapshot_id = (
448- snapshot .snapshot_id if (snapshot := self ._transaction .table_metadata .snapshot_by_name (self ._target_branch )) else None
449- )
450+ self ._parent_snapshot_id = self ._current_branch_head_id ()
450451 self ._snapshot_id = self ._transaction .table_metadata .new_snapshot_id ()
451452 self ._manifest_num_counter = itertools .count (0 )
452453 self .commit_uuid = uuid .uuid4 ()
0 commit comments