Skip to content

Commit ba1ff56

Browse files
committed
Ensure version is hashable
1 parent 41aa645 commit ba1ff56

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

dfetch/manifest/version.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ def __eq__(self, other: Any) -> bool:
2525
return bool(self.branch == other.branch and self.revision == other.revision)
2626

2727
def __hash__(self) -> int:
28-
"""Hash based on the underlying tuple value."""
29-
return tuple.__hash__(self)
28+
"""Hash only fields that determine equality."""
29+
if self.tag:
30+
return hash(self.tag)
31+
return hash((self.branch, self.revision))
3032

3133
@property
3234
def field(self) -> tuple[str, str]:

tests/test_project_version.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,8 @@ def test_version_comparison_with_non_version(other):
219219

220220
def test_version_remains_hashable():
221221
"""Defining __eq__ must not break hashing/set membership."""
222-
version = Version(tag="1.0")
223-
assert version in {version}
222+
v1 = Version(tag="1.0")
223+
v2 = Version(tag="1.0")
224+
assert v1 == v2
225+
assert hash(v1) == hash(v2)
226+
assert v2 in {v1}

0 commit comments

Comments
 (0)