Skip to content

Commit ee75a1c

Browse files
committed
Ensure default timeout is used by API Client
The `from_env` method on the `docker` module passed `None` as the value for the `timeout` keyword argument which overrode the default value in the initialiser, taken from `constants` module. This sets the default in the initialiser to `None` and adds logic to set that, in the same way that `version` is handled. Signed-off-by: grahamlyons <graham@grahamlyons.com>
1 parent dc2b24d commit ee75a1c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

docker/api/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ class APIClient(
8383
configuration.
8484
user_agent (str): Set a custom user agent for requests to the server.
8585
"""
86-
def __init__(self, base_url=None, version=None,
87-
timeout=DEFAULT_TIMEOUT_SECONDS, tls=False,
86+
def __init__(self, base_url=None, version=None, timeout=None, tls=False,
8887
user_agent=DEFAULT_USER_AGENT, num_pools=DEFAULT_NUM_POOLS):
8988
super(APIClient, self).__init__()
9089

@@ -94,7 +93,11 @@ def __init__(self, base_url=None, version=None,
9493
)
9594

9695
self.base_url = base_url
97-
self.timeout = timeout
96+
if timeout is not None:
97+
self.timeout = timeout
98+
else:
99+
self.timeout = DEFAULT_TIMEOUT_SECONDS
100+
98101
self.headers['User-Agent'] = user_agent
99102

100103
self._auth_configs = auth.load_config()

tests/unit/client_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import datetime
22
import docker
33
from docker.utils import kwargs_from_env
4+
from docker.constants import (
5+
DEFAULT_DOCKER_API_VERSION, DEFAULT_TIMEOUT_SECONDS
6+
)
47
import os
58
import unittest
69

@@ -96,3 +99,13 @@ def test_from_env_with_version(self):
9699
client = docker.from_env(version='2.32')
97100
self.assertEqual(client.api.base_url, "https://192.168.59.103:2376")
98101
self.assertEqual(client.api._version, '2.32')
102+
103+
def test_from_env_without_version_uses_default(self):
104+
client = docker.from_env()
105+
106+
self.assertEqual(client.api._version, DEFAULT_DOCKER_API_VERSION)
107+
108+
def test_from_env_without_timeout_uses_default(self):
109+
client = docker.from_env()
110+
111+
self.assertEqual(client.api.timeout, DEFAULT_TIMEOUT_SECONDS)

0 commit comments

Comments
 (0)