Skip to content

Commit 1adc9e3

Browse files
fcurellashin-
authored andcommitted
Make resources hashable, so that they can be added to sets
Signed-off-by: Flavio Curella <[email protected]>
1 parent 17c6e05 commit 1adc9e3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

docker/models/resource.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def __repr__(self):
2323
def __eq__(self, other):
2424
return isinstance(other, self.__class__) and self.id == other.id
2525

26+
def __hash__(self):
27+
return hash("%s:%s" % (self.__class__.__name__, self.id))
28+
2629
@property
2730
def id(self):
2831
"""

tests/unit/models_resources_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ def test_reload(self):
1212
container.reload()
1313
assert client.api.inspect_container.call_count == 2
1414
assert container.attrs['Name'] == "foobar"
15+
16+
def test_hash(self):
17+
client = make_fake_client()
18+
container1 = client.containers.get(FAKE_CONTAINER_ID)
19+
my_set = set([container1])
20+
assert len(my_set) == 1
21+
22+
container2 = client.containers.get(FAKE_CONTAINER_ID)
23+
my_set.add(container2)
24+
assert len(my_set) == 1
25+
26+
image1 = client.images.get(FAKE_CONTAINER_ID)
27+
my_set.add(image1)
28+
assert len(my_set) == 2

0 commit comments

Comments
 (0)