Skip to content

Commit 6aec90a

Browse files
committed
Fix Ruff B904s (be explicit about exception causes)
Signed-off-by: Aarni Koskela <akx@iki.fi>
1 parent 8447f7b commit 6aec90a

File tree

14 files changed

+31
-32
lines changed

14 files changed

+31
-32
lines changed

docker/api/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ def __init__(self, base_url=None, version=None,
160160
base_url, timeout, pool_connections=num_pools,
161161
max_pool_size=max_pool_size
162162
)
163-
except NameError:
163+
except NameError as err:
164164
raise DockerException(
165165
'Install pypiwin32 package to enable npipe:// support'
166-
)
166+
) from err
167167
self.mount('http+docker://', self._custom_adapter)
168168
self.base_url = 'http+docker://localnpipe'
169169
elif base_url.startswith('ssh://'):
@@ -172,10 +172,10 @@ def __init__(self, base_url=None, version=None,
172172
base_url, timeout, pool_connections=num_pools,
173173
max_pool_size=max_pool_size, shell_out=use_ssh_client
174174
)
175-
except NameError:
175+
except NameError as err:
176176
raise DockerException(
177177
'Install paramiko package to enable ssh:// support'
178-
)
178+
) from err
179179
self.mount('http+docker://ssh', self._custom_adapter)
180180
self._unmount('http://', 'https://')
181181
self.base_url = 'http+docker://ssh'
@@ -211,15 +211,15 @@ def __init__(self, base_url=None, version=None,
211211
def _retrieve_server_version(self):
212212
try:
213213
return self.version(api_version=False)["ApiVersion"]
214-
except KeyError:
214+
except KeyError as ke:
215215
raise DockerException(
216216
'Invalid response from docker daemon: key "ApiVersion"'
217217
' is missing.'
218-
)
218+
) from ke
219219
except Exception as e:
220220
raise DockerException(
221221
f'Error while fetching server API version: {e}'
222-
)
222+
) from e
223223

224224
def _set_request_timeout(self, kwargs):
225225
"""Prepare the kwargs for an HTTP request by inserting the timeout

docker/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _resolve_authconfig_credstore(self, registry, credstore_name):
268268
except credentials.StoreError as e:
269269
raise errors.DockerException(
270270
f'Credentials store error: {repr(e)}'
271-
)
271+
) from e
272272

273273
def _get_store_instance(self, name):
274274
if name not in self._stores:

docker/context/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def contexts(cls):
114114
except Exception as e:
115115
raise errors.ContextException(
116116
f"Failed to load metafile {filename}: {e}",
117-
)
117+
) from e
118118

119119
contexts = [cls.DEFAULT_CONTEXT]
120120
for name in names:

docker/context/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _load_meta(cls, name):
9999
# unknown format
100100
raise Exception(
101101
f"Detected corrupted meta file for context {name} : {e}"
102-
)
102+
) from e
103103

104104
# for docker endpoints, set defaults for
105105
# Host and SkipTLSVerify fields

docker/credentials/store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def _execute(self, subcmd, data_input):
8080
[self.exe, subcmd], input=data_input, env=env,
8181
)
8282
except subprocess.CalledProcessError as e:
83-
raise errors.process_store_error(e, self.program)
83+
raise errors.process_store_error(e, self.program) from e
8484
except OSError as e:
8585
if e.errno == errno.ENOENT:
8686
raise errors.StoreError(
8787
f'{self.program} not installed or not available in PATH'
88-
)
88+
) from e
8989
else:
9090
raise errors.StoreError(
9191
f'Unexpected OS error "{e.strerror}", errno={e.errno}'
92-
)
92+
) from e
9393
return output

docker/models/containers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ def labels(self):
4747
try:
4848
result = self.attrs['Config'].get('Labels')
4949
return result or {}
50-
except KeyError:
50+
except KeyError as ke:
5151
raise DockerException(
5252
'Label data is not available for sparse objects. Call reload()'
5353
' to retrieve all information'
54-
)
54+
) from ke
5555

5656
@property
5757
def status(self):

docker/tls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, client_cert=None, ca_cert=None, verify=None,
5555
raise errors.TLSParameterError(
5656
'client_cert must be a tuple of'
5757
' (client certificate, key file)'
58-
)
58+
) from None
5959

6060
if not (tls_cert and tls_key) or (not os.path.isfile(tls_cert) or
6161
not os.path.isfile(tls_key)):

docker/transport/npipeconn.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,16 @@ def _get_conn(self, timeout):
4646
conn = None
4747
try:
4848
conn = self.pool.get(block=self.block, timeout=timeout)
49-
50-
except AttributeError: # self.pool is None
51-
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.")
49+
except AttributeError as ae: # self.pool is None
50+
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.") from ae
5251

5352
except queue.Empty:
5453
if self.block:
5554
raise urllib3.exceptions.EmptyPoolError(
5655
self,
5756
"Pool reached maximum size and no more "
5857
"connections are allowed."
59-
)
58+
) from None
6059
# Oh well, we'll create a new connection then
6160

6261
return conn or self._new_conn()

docker/transport/sshconn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ def _get_conn(self, timeout):
141141
try:
142142
conn = self.pool.get(block=self.block, timeout=timeout)
143143

144-
except AttributeError: # self.pool is None
145-
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.")
144+
except AttributeError as ae: # self.pool is None
145+
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.") from ae
146146

147147
except queue.Empty:
148148
if self.block:
149149
raise urllib3.exceptions.EmptyPoolError(
150150
self,
151151
"Pool reached maximum size and no more "
152152
"connections are allowed."
153-
)
153+
) from None
154154
# Oh well, we'll create a new connection then
155155

156156
return conn or self._new_conn()

docker/types/daemon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def __next__(self):
2828
try:
2929
return next(self._stream)
3030
except urllib3.exceptions.ProtocolError:
31-
raise StopIteration
31+
raise StopIteration from None
3232
except OSError:
33-
raise StopIteration
33+
raise StopIteration from None
3434

3535
next = __next__
3636

0 commit comments

Comments
 (0)