Skip to content

Commit

Permalink
Ensure audb.Repository is hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Dec 15, 2024
1 parent d7b87e8 commit 2902074
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions audb/core/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def __eq__(self, other) -> bool:
"""
return str(self) == str(other)

def __hash__(self):
"""Hash of repository.
Returns:
hash of repository
"""
return hash(str(self))

def __repr__(self): # noqa: D105
return (
f"Repository("
Expand Down
11 changes: 11 additions & 0 deletions tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ def test_repository_eq(repository1, repository2, expected):
assert (repository1 == repository2) == expected


@pytest.mark.parametrize(
"repository",
[audb.Repository("repo", "host", "file-system")],
)
def test_repository_hash(repository):
"""Test Repository object is hashable."""
assert isinstance(hash(repository), int)
# set needs `__hash__` to exist to work
assert set([repository]) == set([str(repository)])


@pytest.mark.parametrize(
"backend, host, repo, expected",
[
Expand Down

0 comments on commit 2902074

Please sign in to comment.