Skip to content

Commit 08872a3

Browse files
aanandshin-
authored andcommitted
Show a helpful warning when people try to call client.containers()
People upgrading to docker-py 2.0 without being aware of the new client API will likely try to call the old `containers()` method. This adds a helpful warning telling them to use APIClient to get the old API. Signed-off-by: Aanand Prasad <[email protected]>
1 parent d19e8e6 commit 08872a3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

docker/models/resource.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def __init__(self, client=None):
6060
#: is on.
6161
self.client = client
6262

63+
def __call__(self, *args, **kwargs):
64+
raise TypeError(
65+
"'{}' object is not callable. You might be trying to use the old "
66+
"(pre-2.0) API - use docker.APIClient if so."
67+
.format(self.__class__.__name__))
68+
6369
def list(self):
6470
raise NotImplementedError
6571

tests/unit/client_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import docker
3+
from docker.utils import kwargs_from_env
34
import os
45
import unittest
56

@@ -59,6 +60,16 @@ def test_call_api_client_method(self):
5960
assert "'Client' object has no attribute 'abcdef'" in s
6061
assert "this method is now on the object APIClient" not in s
6162

63+
def test_call_containers(self):
64+
client = docker.Client(**kwargs_from_env())
65+
66+
with self.assertRaises(TypeError) as cm:
67+
client.containers()
68+
69+
s = str(cm.exception)
70+
assert "'ContainerCollection' object is not callable" in s
71+
assert "docker.APIClient" in s
72+
6273

6374
class FromEnvTest(unittest.TestCase):
6475

0 commit comments

Comments
 (0)