diff --git a/docker/models/resource.py b/docker/models/resource.py index 95712aef..ed3900af 100644 --- a/docker/models/resource.py +++ b/docker/models/resource.py @@ -23,6 +23,9 @@ class Model(object): def __eq__(self, other): return isinstance(other, self.__class__) and self.id == other.id + def __hash__(self): + return hash("%s:%s" % (self.__class__.__name__, self.id)) + @property def id(self): """ diff --git a/tests/unit/models_resources_test.py b/tests/unit/models_resources_test.py index 25c6a3ed..5af24ee6 100644 --- a/tests/unit/models_resources_test.py +++ b/tests/unit/models_resources_test.py @@ -12,3 +12,17 @@ class ModelTest(unittest.TestCase): container.reload() assert client.api.inspect_container.call_count == 2 assert container.attrs['Name'] == "foobar" + + def test_hash(self): + client = make_fake_client() + container1 = client.containers.get(FAKE_CONTAINER_ID) + my_set = set([container1]) + assert len(my_set) == 1 + + container2 = client.containers.get(FAKE_CONTAINER_ID) + my_set.add(container2) + assert len(my_set) == 1 + + image1 = client.images.get(FAKE_CONTAINER_ID) + my_set.add(image1) + assert len(my_set) == 2