Skip to content

Commit 658cb3d

Browse files
committed
Handle null case when encoding / decoding sha1 in BaseFileVersion
1 parent a7cd185 commit 658cb3d

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

b2sdk/_internal/file_version.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,19 @@ def __init__(
9595
self.mod_time_millis = self.upload_timestamp
9696

9797
@classmethod
98-
def _decode_content_sha1(cls, content_sha1):
98+
def _decode_content_sha1(cls, content_sha1: str | None) -> tuple[str | None, bool]:
99+
if content_sha1 is None:
100+
return None, True
99101
if content_sha1.startswith(UNVERIFIED_CHECKSUM_PREFIX):
100102
return content_sha1[len(UNVERIFIED_CHECKSUM_PREFIX) :], False
101103
return content_sha1, True
102104

103105
@classmethod
104-
def _encode_content_sha1(cls, content_sha1, content_sha1_verified):
106+
def _encode_content_sha1(
107+
cls, content_sha1: str | None, content_sha1_verified: bool
108+
) -> str | None:
109+
if content_sha1 is None:
110+
return None
105111
if not content_sha1_verified:
106112
return f'{UNVERIFIED_CHECKSUM_PREFIX}{content_sha1}'
107113
return content_sha1

changelog.d/553.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Handle null case when encoding / decoding `content_sha1` in BaseFileVersion.

test/unit/file_version/test_file_version.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ def test_clone_file_version_and_download_version(self):
117117
assert isinstance(cloned, DownloadVersion)
118118
assert cloned.as_dict() == {**download_version.as_dict(), 'legalHold': LegalHold.OFF.value}
119119

120+
def test_clone_file_version_with_unknown_sha1(self):
121+
cloned = self.file_version._clone(content_sha1=None)
122+
123+
assert isinstance(cloned, VFileVersion)
124+
assert cloned.content_sha1 is None
125+
assert cloned.content_sha1_verified is True
126+
assert 'contentSha1' not in cloned.as_dict()
127+
120128
def test_update_legal_hold(self):
121129
new_file_version = self.file_version.update_legal_hold(LegalHold.ON)
122130
assert isinstance(new_file_version, VFileVersion)

0 commit comments

Comments
 (0)