Skip to content

Commit 2fcd759

Browse files
authored
Handle flake8-errmsg (#1155)
1 parent 898d180 commit 2fcd759

File tree

15 files changed

+120
-94
lines changed

15 files changed

+120
-94
lines changed

jupyter_server/auth/identity.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def fill_defaults(self):
7272

7373
# username is the only truly required field
7474
if not self.username:
75-
raise ValueError(f"user.username must not be empty: {self}")
75+
msg = f"user.username must not be empty: {self}"
76+
raise ValueError(msg)
7677

7778
# derive name fields from username -> name -> display name
7879
if not self.name:
@@ -103,10 +104,12 @@ def _backward_compat_user(got_user: Any) -> User:
103104
kwargs[field] = got_user[field]
104105
try:
105106
return User(**kwargs)
106-
except TypeError as e:
107-
raise ValueError(f"Unrecognized user: {got_user}") from e
107+
except TypeError:
108+
msg = f"Unrecognized user: {got_user}"
109+
raise ValueError(msg) from None
108110
else:
109-
raise ValueError(f"Unrecognized user: {got_user}")
111+
msg = f"Unrecognized user: {got_user}"
112+
raise ValueError(msg)
110113

111114

112115
class IdentityProvider(LoggingConfigurable):

jupyter_server/auth/security.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def passwd(passphrase=None, algorithm="argon2"):
5454
else:
5555
warnings.warn("Passwords do not match.")
5656
else:
57-
raise ValueError("No matching passwords found. Giving up.")
57+
msg = "No matching passwords found. Giving up."
58+
raise ValueError(msg)
5859

5960
if algorithm == "argon2":
6061
import argon2

jupyter_server/extension/handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ def static_url(self, path, include_host=None, **kwargs):
100100
self.require_setting(key, "static_url") # type:ignore[attr-defined]
101101
except Exception as e:
102102
if key in self.settings: # type:ignore[attr-defined]
103-
raise Exception(
103+
msg = (
104104
"This extension doesn't have any static paths listed. Check that the "
105105
"extension's `static_paths` trait is set."
106-
) from e
106+
)
107+
raise Exception(msg) from None
107108
else:
108109
raise e
109110

jupyter_server/extension/manager.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ def _valid_metadata(self, proposed):
2727
# Verify that the metadata has a "name" key.
2828
try:
2929
self._module_name = metadata["module"]
30-
except KeyError as e:
31-
raise ExtensionMetadataError(
32-
"There is no 'module' key in the extension's metadata packet."
33-
) from e
30+
except KeyError:
31+
msg = "There is no 'module' key in the extension's metadata packet."
32+
raise ExtensionMetadataError(msg) from None
3433

3534
try:
3635
self._module = importlib.import_module(self._module_name)
37-
except ImportError as e:
38-
raise ExtensionModuleNotFound(
39-
"The submodule '{}' could not be found. Are you "
40-
"sure the extension is installed?".format(self._module_name)
41-
) from e
36+
except ImportError:
37+
msg = (
38+
f"The submodule '{self._module_name}' could not be found. Are you "
39+
"sure the extension is installed?"
40+
)
41+
raise ExtensionModuleNotFound(msg) from None
4242
# If the metadata includes an ExtensionApp, create an instance.
4343
if "app" in metadata:
4444
self._app = metadata["app"]()
@@ -174,10 +174,11 @@ def _validate_name(self, proposed):
174174
try:
175175
self._module, self._metadata = get_metadata(name)
176176
except ImportError as e:
177-
raise ExtensionModuleNotFound(
178-
"The module '{name}' could not be found ({e}). Are you "
179-
"sure the extension is installed?".format(name=name, e=e)
180-
) from e
177+
msg = (
178+
f"The module '{name}' could not be found ({e}). Are you "
179+
"sure the extension is installed?"
180+
)
181+
raise ExtensionModuleNotFound(msg) from None
181182
# Create extension point interfaces for each extension path.
182183
for m in self._metadata:
183184
point = ExtensionPoint(metadata=m)

jupyter_server/extension/serverextension.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ def list_server_extensions(self):
339339
self.log.info(f" - Validating {name}...")
340340
extension = ExtensionPackage(name=name, enabled=enabled)
341341
if not extension.validate():
342-
raise ValueError("validation failed")
342+
msg = "validation failed"
343+
raise ValueError(msg)
343344
version = extension.version
344345
self.log.info(f" {name} {version} {GREEN_OK}")
345346
except Exception as err:

jupyter_server/extension/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ def get_loader(obj, logger=None):
4646
"of Jupyter Server.".format(name=obj),
4747
DeprecationWarning,
4848
)
49-
except Exception as e:
50-
raise ExtensionLoadingError("_load_jupyter_server_extension function was not found.") from e
49+
except Exception:
50+
msg = "_load_jupyter_server_extension function was not found."
51+
raise ExtensionLoadingError(msg) from None
5152
return func
5253

5354

jupyter_server/gateway/managers.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,8 @@ async def get_kernel_spec(self, kernel_name, **kwargs):
283283
if error.status_code == 404:
284284
# Convert not found to KeyError since that's what the Notebook handler expects
285285
# message is not used, but might as well make it useful for troubleshooting
286-
raise KeyError(
287-
"kernelspec {kernel_name} not found on Gateway server at: {gateway_url}".format(
288-
kernel_name=kernel_name,
289-
gateway_url=GatewayClient.instance().url,
290-
)
291-
) from error
286+
msg = f"kernelspec {kernel_name} not found on Gateway server at: {GatewayClient.instance().url}"
287+
raise KeyError(msg) from None
292288
else:
293289
raise
294290
else:
@@ -565,15 +561,17 @@ async def _async_get(self, timeout=None):
565561
if timeout is None:
566562
timeout = float("inf")
567563
elif timeout < 0:
568-
raise ValueError("'timeout' must be a non-negative number")
564+
msg = "'timeout' must be a non-negative number"
565+
raise ValueError(msg)
569566
end_time = monotonic() + timeout
570567

571568
while True:
572569
try:
573570
return self.get(block=False)
574-
except Empty as e:
571+
except Empty:
575572
if self.response_router_finished:
576-
raise RuntimeError("Response router had finished") from e
573+
msg = "Response router had finished"
574+
raise RuntimeError(msg) from None
577575
if monotonic() > end_time:
578576
raise
579577
await asyncio.sleep(0)

jupyter_server/serverapp.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,13 @@ def init_handlers(self, default_services, settings):
419419
for loc in locations:
420420
handlers.extend(load_handlers(loc))
421421
else:
422-
raise Exception(
423-
"{} is not recognized as a jupyter_server "
422+
msg = (
423+
f"{service} is not recognized as a jupyter_server "
424424
"service. If this is a custom service, "
425425
"try adding it to the "
426-
"`extra_services` list.".format(service)
426+
"`extra_services` list."
427427
)
428+
raise Exception(msg)
428429

429430
# Add extra handlers from contents manager.
430431
handlers.extend(settings["contents_manager"].get_extra_handlers())
@@ -2375,23 +2376,25 @@ def http_server(self):
23752376
"""An instance of Tornado's HTTPServer class for the Server Web Application."""
23762377
try:
23772378
return self._http_server
2378-
except AttributeError as e:
2379-
raise AttributeError(
2379+
except AttributeError:
2380+
msg = (
23802381
"An HTTPServer instance has not been created for the "
23812382
"Server Web Application. To create an HTTPServer for this "
23822383
"application, call `.init_httpserver()`."
2383-
) from e
2384+
)
2385+
raise AttributeError(msg) from None
23842386

23852387
def init_httpserver(self):
23862388
"""Creates an instance of a Tornado HTTPServer for the Server Web Application
23872389
and sets the http_server attribute.
23882390
"""
23892391
# Check that a web_app has been initialized before starting a server.
23902392
if not hasattr(self, "web_app"):
2391-
raise AttributeError(
2393+
msg = (
23922394
"A tornado web application has not be initialized. "
23932395
"Try calling `.init_webapp()` first."
23942396
)
2397+
raise AttributeError(msg)
23952398

23962399
# Create an instance of the server.
23972400
self._http_server = httpserver.HTTPServer(

jupyter_server/services/contents/checkpoints.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ class Checkpoints(LoggingConfigurable):
2222

2323
def create_checkpoint(self, contents_mgr, path):
2424
"""Create a checkpoint."""
25-
raise NotImplementedError("must be implemented in a subclass")
25+
raise NotImplementedError
2626

2727
def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
2828
"""Restore a checkpoint"""
29-
raise NotImplementedError("must be implemented in a subclass")
29+
raise NotImplementedError
3030

3131
def rename_checkpoint(self, checkpoint_id, old_path, new_path):
3232
"""Rename a single checkpoint from old_path to new_path."""
33-
raise NotImplementedError("must be implemented in a subclass")
33+
raise NotImplementedError
3434

3535
def delete_checkpoint(self, checkpoint_id, path):
3636
"""delete a checkpoint for a file"""
37-
raise NotImplementedError("must be implemented in a subclass")
37+
raise NotImplementedError
3838

3939
def list_checkpoints(self, path):
4040
"""Return a list of checkpoints for a given file"""
41-
raise NotImplementedError("must be implemented in a subclass")
41+
raise NotImplementedError
4242

4343
def rename_all_checkpoints(self, old_path, new_path):
4444
"""Rename all checkpoints for old_path to new_path."""
@@ -107,14 +107,14 @@ def create_file_checkpoint(self, content, format, path):
107107
108108
Returns a checkpoint model for the new checkpoint.
109109
"""
110-
raise NotImplementedError("must be implemented in a subclass")
110+
raise NotImplementedError
111111

112112
def create_notebook_checkpoint(self, nb, path):
113113
"""Create a checkpoint of the current state of a file
114114
115115
Returns a checkpoint model for the new checkpoint.
116116
"""
117-
raise NotImplementedError("must be implemented in a subclass")
117+
raise NotImplementedError
118118

119119
def get_file_checkpoint(self, checkpoint_id, path):
120120
"""Get the content of a checkpoint for a non-notebook file.
@@ -126,7 +126,7 @@ def get_file_checkpoint(self, checkpoint_id, path):
126126
'format': {'text','base64'},
127127
}
128128
"""
129-
raise NotImplementedError("must be implemented in a subclass")
129+
raise NotImplementedError
130130

131131
def get_notebook_checkpoint(self, checkpoint_id, path):
132132
"""Get the content of a checkpoint for a notebook.
@@ -137,7 +137,7 @@ def get_notebook_checkpoint(self, checkpoint_id, path):
137137
'content': <output of nbformat.read>,
138138
}
139139
"""
140-
raise NotImplementedError("must be implemented in a subclass")
140+
raise NotImplementedError
141141

142142

143143
class AsyncCheckpoints(Checkpoints):
@@ -147,23 +147,23 @@ class AsyncCheckpoints(Checkpoints):
147147

148148
async def create_checkpoint(self, contents_mgr, path):
149149
"""Create a checkpoint."""
150-
raise NotImplementedError("must be implemented in a subclass")
150+
raise NotImplementedError
151151

152152
async def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
153153
"""Restore a checkpoint"""
154-
raise NotImplementedError("must be implemented in a subclass")
154+
raise NotImplementedError
155155

156156
async def rename_checkpoint(self, checkpoint_id, old_path, new_path):
157157
"""Rename a single checkpoint from old_path to new_path."""
158-
raise NotImplementedError("must be implemented in a subclass")
158+
raise NotImplementedError
159159

160160
async def delete_checkpoint(self, checkpoint_id, path):
161161
"""delete a checkpoint for a file"""
162-
raise NotImplementedError("must be implemented in a subclass")
162+
raise NotImplementedError
163163

164164
async def list_checkpoints(self, path):
165165
"""Return a list of checkpoints for a given file"""
166-
raise NotImplementedError("must be implemented in a subclass")
166+
raise NotImplementedError
167167

168168
async def rename_all_checkpoints(self, old_path, new_path):
169169
"""Rename all checkpoints for old_path to new_path."""
@@ -217,14 +217,14 @@ async def create_file_checkpoint(self, content, format, path):
217217
218218
Returns a checkpoint model for the new checkpoint.
219219
"""
220-
raise NotImplementedError("must be implemented in a subclass")
220+
raise NotImplementedError
221221

222222
async def create_notebook_checkpoint(self, nb, path):
223223
"""Create a checkpoint of the current state of a file
224224
225225
Returns a checkpoint model for the new checkpoint.
226226
"""
227-
raise NotImplementedError("must be implemented in a subclass")
227+
raise NotImplementedError
228228

229229
async def get_file_checkpoint(self, checkpoint_id, path):
230230
"""Get the content of a checkpoint for a non-notebook file.
@@ -236,7 +236,7 @@ async def get_file_checkpoint(self, checkpoint_id, path):
236236
'format': {'text','base64'},
237237
}
238238
"""
239-
raise NotImplementedError("must be implemented in a subclass")
239+
raise NotImplementedError
240240

241241
async def get_notebook_checkpoint(self, checkpoint_id, path):
242242
"""Get the content of a checkpoint for a notebook.
@@ -247,4 +247,4 @@ async def get_notebook_checkpoint(self, checkpoint_id, path):
247247
'content': <output of nbformat.read>,
248248
}
249249
"""
250-
raise NotImplementedError("must be implemented in a subclass")
250+
raise NotImplementedError

0 commit comments

Comments
 (0)