Skip to content

Commit

Permalink
Add helpful error for APIClient methods on Client
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Firshman <ben@firshman.co.uk>
  • Loading branch information
bfirsh committed Nov 18, 2016
1 parent c3efab8 commit a55fd23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,14 @@ def version(self, *args, **kwargs):
return self.api.version(*args, **kwargs)
version.__doc__ = APIClient.version.__doc__

def __getattr__(self, name):
s = ["'Client' object has no attribute '{}'".format(name)]
# If a user calls a method on APIClient, they
if hasattr(APIClient, name):
s.append("In docker-py 2.0, this method is now on the object "
"APIClient. See the low-level API section of the "
"documentation for more details.".format(name))
raise AttributeError(' '.join(s))


from_env = Client.from_env
14 changes: 14 additions & 0 deletions tests/unit/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ def test_version(self, mock_func):
assert client.version() == mock_func.return_value
mock_func.assert_called_with()

def test_call_api_client_method(self):
client = docker.from_env()
with self.assertRaises(AttributeError) as cm:
client.create_container()
s = str(cm.exception)
assert "'Client' object has no attribute 'create_container'" in s
assert "this method is now on the object APIClient" in s

with self.assertRaises(AttributeError) as cm:
client.abcdef()
s = str(cm.exception)
assert "'Client' object has no attribute 'abcdef'" in s
assert "this method is now on the object APIClient" not in s


class FromEnvTest(unittest.TestCase):

Expand Down

0 comments on commit a55fd23

Please sign in to comment.