Skip to content

Commit

Permalink
Bump ruff from 0.0.270 to 0.0.290 (#1331)
Browse files Browse the repository at this point in the history
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Luciano Resende <lresende@apple.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Luciano Resende <lresende@apple.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 23, 2023
1 parent cdbe1ca commit 9424e3f
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repos:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.270
rev: v0.0.290
hooks:
- id: ruff
args: ["--fix"]
4 changes: 1 addition & 3 deletions enterprise_gateway/client/gateway_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ def interrupt(self):
self.log.debug(f"Kernel {self.kernel_id} interrupted")
return True
else:
msg = "Unexpected response interrupting kernel {}: {}".format(
self.kernel_id, response.content
)
msg = f"Unexpected response interrupting kernel {self.kernel_id}: {response.content}"
raise RuntimeError(msg)

def restart(self, timeout=REQUEST_TIMEOUT):
Expand Down
8 changes: 4 additions & 4 deletions enterprise_gateway/enterprisegatewayapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
import time
import weakref
from typing import List, Optional
from typing import ClassVar, List, Optional

from jupyter_client.kernelspec import KernelSpecManager
from jupyter_core.application import JupyterApp, base_aliases
Expand Down Expand Up @@ -78,7 +78,7 @@ class EnterpriseGatewayApp(EnterpriseGatewayConfigMixin, JupyterApp):
"""

# Also include when generating help options
classes = [
classes: ClassVar = [
KernelSpecCache,
FileKernelSessionManager,
WebhookKernelSessionManager,
Expand Down Expand Up @@ -369,7 +369,7 @@ def _signal_stop(self, sig, frame) -> None:
self.io_loop.add_callback_from_signal(self.io_loop.stop)

_last_config_update = int(time.time())
_dynamic_configurables = {}
_dynamic_configurables: ClassVar = {}

def update_dynamic_configurables(self) -> bool:
"""
Expand Down Expand Up @@ -403,7 +403,7 @@ def update_dynamic_configurables(self) -> bool:

self.log.info(
"Configuration file changes detected. Instances for the following "
"configurables have been updated: {}".format(configs)
f"configurables have been updated: {configs}"
)
return updated

Expand Down
4 changes: 2 additions & 2 deletions enterprise_gateway/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
from distutils.util import strtobool
from http.client import responses
from typing import Any, Awaitable, Dict, List, Optional, Set
from typing import Any, Awaitable, ClassVar, Dict, List, Optional, Set

from tornado import web
from tornado.log import LogFormatter
Expand All @@ -36,7 +36,7 @@ class CORSMixin:
Mixes CORS headers into tornado.web.RequestHandlers.
"""

SETTINGS_TO_HEADERS = {
SETTINGS_TO_HEADERS: ClassVar = {
"eg_allow_credentials": "Access-Control-Allow-Credentials",
"eg_allow_headers": "Access-Control-Allow-Headers",
"eg_allow_methods": "Access-Control-Allow-Methods",
Expand Down
10 changes: 4 additions & 6 deletions enterprise_gateway/services/kernels/remotemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import signal
import time
import uuid
from typing import Any
from typing import Any, ClassVar

from jupyter_client.ioloop.manager import AsyncIOLoopKernelManager
from jupyter_client.kernelspec import KernelSpec
Expand Down Expand Up @@ -136,7 +136,7 @@ class TrackPendingRequests:
"""

_pending_requests_all = 0
_pending_requests_user = {}
_pending_requests_user: ClassVar = {}

def increment(self, username: str) -> None:
"""Increment the requests for a username."""
Expand Down Expand Up @@ -570,9 +570,7 @@ async def _launch_kernel(
del env["KG_AUTH_TOKEN"]

self.log.debug(
"Launching kernel: '{}' with command: {}".format(
self.kernel_spec.display_name, kernel_cmd
)
f"Launching kernel: '{self.kernel_spec.display_name}' with command: {kernel_cmd}"
)

proxy = await self.process_proxy.launch_process(kernel_cmd, **kwargs)
Expand Down Expand Up @@ -660,7 +658,7 @@ async def signal_kernel(self, signum: int) -> None:
if alt_sigint:
try:
sig_value = getattr(signal, alt_sigint)
if type(sig_value) is int: # Python 2
if isinstance(sig_value, int): # Python 2
self.sigint_value = sig_value
else: # Python 3
self.sigint_value = sig_value.value
Expand Down
2 changes: 1 addition & 1 deletion enterprise_gateway/services/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def apply_user_filter(
kernelspec_model: Dict[str, object],
global_authorized_list: Set,
global_unauthorized_list: Set,
kernel_user: str = None,
kernel_user: Optional[str] = None,
) -> Optional[Dict[str, object]]:
"""
If authorization lists are configured - either within the kernelspec or globally, ensure
Expand Down
26 changes: 7 additions & 19 deletions enterprise_gateway/services/kernelspecs/kernelspec_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


import os
from typing import Dict, Optional, Union
from typing import ClassVar, Dict, Optional, Union

from jupyter_client.kernelspec import KernelSpec
from jupyter_server.utils import ensure_async
Expand Down Expand Up @@ -105,11 +105,7 @@ def get_item(self, kernel_name: str) -> Optional[KernelSpec]:
pass
if not kernelspec:
self.cache_misses += 1
self.log.debug(
"Cache miss ({misses}) for kernelspec: {kernel_name}".format(
misses=self.cache_misses, kernel_name=kernel_name
)
)
self.log.debug(f"Cache miss ({self.cache_misses}) for kernelspec: {kernel_name}")
return kernelspec

def get_all_items(self) -> Dict[str, CacheItemType]:
Expand Down Expand Up @@ -147,11 +143,7 @@ def put_item(self, kernel_name: str, cache_item: Union[KernelSpec, CacheItemType
observed_dir = os.path.dirname(resource_dir)
if observed_dir not in self.observed_dirs:
# New directory to watch, schedule it...
self.log.debug(
"KernelSpecCache: observing directory: {observed_dir}".format(
observed_dir=observed_dir
)
)
self.log.debug(f"KernelSpecCache: observing directory: {observed_dir}")
self.observed_dirs.add(observed_dir)
self.observer.schedule(KernelSpecChangeHandler(self), observed_dir, recursive=True)

Expand Down Expand Up @@ -186,19 +178,15 @@ def _initialize(self):
for kernel_dir in self.kernel_spec_manager.kernel_dirs:
if kernel_dir not in self.observed_dirs:
if os.path.exists(kernel_dir):
self.log.info(
"KernelSpecCache: observing directory: {kernel_dir}".format(
kernel_dir=kernel_dir
)
)
self.log.info(f"KernelSpecCache: observing directory: {kernel_dir}")
self.observed_dirs.add(kernel_dir)
self.observer.schedule(
KernelSpecChangeHandler(self), kernel_dir, recursive=True
)
else:
self.log.warning(
"KernelSpecCache: kernel_dir '{kernel_dir}' does not exist"
" and will not be observed.".format(kernel_dir=kernel_dir)
f"KernelSpecCache: kernel_dir '{kernel_dir}' does not exist"
" and will not be observed."
)
self.observer.start()

Expand All @@ -223,7 +211,7 @@ class KernelSpecChangeHandler(FileSystemEventHandler):
# Events related to these files trigger the management of the KernelSpec cache. Should we find
# other files qualify as indicators of a kernel specification's state (like perhaps detached parameter
# files in the future) should be added to this list - at which time it should become configurable.
watched_files = ["kernel.json"]
watched_files: ClassVar = ["kernel.json"]

def __init__(self, kernel_spec_cache: KernelSpecCache, **kwargs):
"""Initialize the handler."""
Expand Down
30 changes: 9 additions & 21 deletions enterprise_gateway/services/processproxies/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import subprocess
import time
from random import randint
from typing import Any
from typing import Any, ClassVar

from jupyter_client import localinterfaces
from jupyter_server.utils import url_unescape
Expand All @@ -32,8 +32,8 @@ class ConductorClusterProcessProxy(RemoteProcessProxy):
Kernel lifecycle management for Conductor clusters.
"""

initial_states = {"SUBMITTED", "WAITING", "RUNNING"}
final_states = {"FINISHED", "KILLED", "RECLAIMED"} # Don't include FAILED state
initial_states: ClassVar = {"SUBMITTED", "WAITING", "RUNNING"}
final_states: ClassVar = {"FINISHED", "KILLED", "RECLAIMED"} # Don't include FAILED state

def __init__(self, kernel_manager: RemoteKernelManager, proxy_config: dict):
"""Initialize the proxy."""
Expand Down Expand Up @@ -227,9 +227,7 @@ def _update_notebook_master_rest_url(self, env_dict: dict) -> None:

if updated_one_notebook_master_rest_url and updated_one_notebook_master_web_submission_url:
self.log.debug(
"Updating KERNEL_NOTEBOOK_MASTER_REST to '{}'.".format(
updated_one_notebook_master_rest_url
)
f"Updating KERNEL_NOTEBOOK_MASTER_REST to '{updated_one_notebook_master_rest_url}'."
)
os.environ["KERNEL_NOTEBOOK_MASTER_REST"] = updated_one_notebook_master_rest_url
env_dict["KERNEL_NOTEBOOK_MASTER_REST"] = updated_one_notebook_master_rest_url
Expand Down Expand Up @@ -416,9 +414,7 @@ async def handle_timeout(self) -> None:
)

if time_interval > self.kernel_launch_timeout:
reason = "Application failed to start within {} seconds.".format(
self.kernel_launch_timeout
)
reason = f"Application failed to start within {self.kernel_launch_timeout} seconds."
error_http_code = 500
if self._get_application_id(True):
if self._query_app_state_by_driver_id(self.driver_id) != "WAITING":
Expand All @@ -435,9 +431,7 @@ async def handle_timeout(self) -> None:
self.application_id, self.kernel_launch_timeout
)
await asyncio.get_event_loop().run_in_executor(None, self.kill)
timeout_message = "KernelID: '{}' launch timeout due to: {}".format(
self.kernel_id, reason
)
timeout_message = f"KernelID: '{self.kernel_id}' launch timeout due to: {reason}"
self.log_and_raise(http_status_code=error_http_code, reason=timeout_message)

def _get_application_id(self, ignore_final_states: bool = False) -> str:
Expand Down Expand Up @@ -473,9 +467,7 @@ def _get_application_id(self, ignore_final_states: bool = False) -> str:
)
else:
self.log.debug(
"ApplicationID not yet assigned for KernelID: '{}' - retrying...".format(
self.kernel_id
)
f"ApplicationID not yet assigned for KernelID: '{self.kernel_id}' - retrying..."
)
return self.application_id

Expand Down Expand Up @@ -525,9 +517,7 @@ def _query_app_by_driver_id(self, driver_id: str) -> dict | None:
response = None if not response or not response["applist"] else response["applist"]
except Exception as e:
self.log.warning(
"Getting application with cmd '{}' failed with exception: '{}'. Continuing...".format(
cmd, e
)
f"Getting application with cmd '{cmd}' failed with exception: '{e}'. Continuing..."
)
return response

Expand Down Expand Up @@ -557,9 +547,7 @@ def _query_app_by_id(self, app_id: str) -> dict | None:
response = None if response is None or not response["applist"] else response["applist"]
except Exception as e:
self.log.warning(
"Getting application with cmd '{}' failed with exception: '{}'. Continuing...".format(
cmd, e
)
f"Getting application with cmd '{cmd}' failed with exception: '{e}'. Continuing..."
)
return response

Expand Down
8 changes: 2 additions & 6 deletions enterprise_gateway/services/processproxies/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,13 @@ def _enforce_prohibited_ids(self, **kwargs: dict[str, Any] | None) -> None:
if kernel_uid in prohibited_uids:
http_status_code = 403
error_message = (
"Kernel's UID value of '{}' has been denied via EG_PROHIBITED_UIDS!".format(
kernel_uid
)
f"Kernel's UID value of '{kernel_uid}' has been denied via EG_PROHIBITED_UIDS!"
)
self.log_and_raise(http_status_code=http_status_code, reason=error_message)
elif kernel_gid in prohibited_gids:
http_status_code = 403
error_message = (
"Kernel's GID value of '{}' has been denied via EG_PROHIBITED_GIDS!".format(
kernel_gid
)
f"Kernel's GID value of '{kernel_gid}' has been denied via EG_PROHIBITED_GIDS!"
)
self.log_and_raise(http_status_code=http_status_code, reason=error_message)

Expand Down
10 changes: 4 additions & 6 deletions enterprise_gateway/services/processproxies/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import signal
from socket import gethostbyname
from subprocess import STDOUT
from typing import Any
from typing import Any, ClassVar

from ..kernels.remotemanager import RemoteKernelManager
from .processproxy import BaseProcessProxyABC, RemoteProcessProxy
Expand All @@ -24,8 +24,8 @@
class TrackKernelOnHost:
"""A class for tracking a kernel on a host."""

_host_kernels = {}
_kernel_host_mapping = {}
_host_kernels: ClassVar = {}
_kernel_host_mapping: ClassVar = {}

def add_kernel_id(self, host: str, kernel_id: str) -> None:
"""Add a kernel to a host."""
Expand Down Expand Up @@ -229,9 +229,7 @@ async def handle_timeout(self) -> None:
self.kernel_launch_timeout, self.assigned_host, self.kernel_log
)
)
timeout_message = "KernelID: '{}' launch timeout due to: {}".format(
self.kernel_id, reason
)
timeout_message = f"KernelID: '{self.kernel_id}' launch timeout due to: {reason}"
await asyncio.get_event_loop().run_in_executor(None, self.kill)
self.log_and_raise(http_status_code=500, reason=timeout_message)

Expand Down
4 changes: 1 addition & 3 deletions enterprise_gateway/services/processproxies/docker_swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,7 @@ def terminate_container_resources(self) -> bool | None:
container.remove(force=True) # Container still exists, attempt forced removal
except Exception as err:
self.log.debug(
"Container termination for container: {} raised exception: {}".format(
container.name, err
)
f"Container termination for container: {container.name} raised exception: {err}"
)
if isinstance(err, NotFound):
pass # okay if its not found
Expand Down
Loading

0 comments on commit 9424e3f

Please sign in to comment.