Skip to content

Commit c7ec378

Browse files
committed
Apply safe ruff auto-fixes
1 parent 4dbf76f commit c7ec378

File tree

16 files changed

+52
-54
lines changed

16 files changed

+52
-54
lines changed

jupyter_server/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
from .base.call_context import CallContext
1818

1919
__all__ = [
20+
"DEFAULT_EVENTS_SCHEMA_PATH",
21+
"DEFAULT_JUPYTER_SERVER_PORT",
2022
"DEFAULT_STATIC_FILES_PATH",
2123
"DEFAULT_TEMPLATE_PATH_LIST",
22-
"DEFAULT_JUPYTER_SERVER_PORT",
2324
"JUPYTER_SERVER_EVENTS_URI",
24-
"DEFAULT_EVENTS_SCHEMA_PATH",
25+
"CallContext",
2526
"__version__",
2627
"version_info",
27-
"CallContext",
2828
]

jupyter_server/auth/decorator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ async def inner(self, *args, **kwargs):
8282
method = action
8383
action = None
8484
# no-arguments `@authorized` decorator called
85-
return cast(FuncT, wrapper(method))
85+
return cast("FuncT", wrapper(method))
8686

87-
return cast(FuncT, wrapper)
87+
return cast("FuncT", wrapper)
8888

8989

9090
def allow_unauthenticated(method: FuncT) -> FuncT:
@@ -111,7 +111,7 @@ def wrapper(self, *args, **kwargs):
111111

112112
setattr(wrapper, "__allow_unauthenticated", True)
113113

114-
return cast(FuncT, wrapper)
114+
return cast("FuncT", wrapper)
115115

116116

117117
def ws_authenticated(method: FuncT) -> FuncT:
@@ -139,4 +139,4 @@ def wrapper(self, *args, **kwargs):
139139

140140
setattr(wrapper, "__allow_unauthenticated", False)
141141

142-
return cast(FuncT, wrapper)
142+
return cast("FuncT", wrapper)

jupyter_server/auth/identity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ async def _get_user(self, handler: web.RequestHandler) -> User | None:
252252
"""Get the user."""
253253
if getattr(handler, "_jupyter_current_user", None):
254254
# already authenticated
255-
return t.cast(User, handler._jupyter_current_user) # type:ignore[attr-defined]
255+
return t.cast("User", handler._jupyter_current_user) # type:ignore[attr-defined]
256256
_token_user: User | None | t.Awaitable[User | None] = self.get_user_token(handler)
257257
if isinstance(_token_user, t.Awaitable):
258258
_token_user = await _token_user
@@ -298,7 +298,7 @@ def update_user(
298298
) -> User:
299299
"""Update user information and persist the user model."""
300300
self.check_update(user_data)
301-
current_user = t.cast(User, handler.current_user)
301+
current_user = t.cast("User", handler.current_user)
302302
updated_user = self.update_user_model(current_user, user_data)
303303
self.persist_user_model(handler)
304304
return updated_user
@@ -585,7 +585,7 @@ def process_login_form(self, handler: web.RequestHandler) -> User | None:
585585
return self.generate_anonymous_user(handler)
586586

587587
if self.token and self.token == typed_password:
588-
return t.cast(User, self.user_for_token(typed_password)) # type:ignore[attr-defined]
588+
return t.cast("User", self.user_for_token(typed_password)) # type:ignore[attr-defined]
589589

590590
return user
591591

jupyter_server/base/handlers.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def json_sys_info():
7575
def log() -> Logger:
7676
"""Get the application log."""
7777
if Application.initialized():
78-
return cast(Logger, Application.instance().log)
78+
return cast("Logger", Application.instance().log)
7979
else:
8080
return app_log
8181

@@ -85,7 +85,7 @@ class AuthenticatedHandler(web.RequestHandler):
8585

8686
@property
8787
def base_url(self) -> str:
88-
return cast(str, self.settings.get("base_url", "/"))
88+
return cast("str", self.settings.get("base_url", "/"))
8989

9090
@property
9191
def content_security_policy(self) -> str:
@@ -95,7 +95,7 @@ def content_security_policy(self) -> str:
9595
"""
9696
if "Content-Security-Policy" in self.settings.get("headers", {}):
9797
# user-specified, don't override
98-
return cast(str, self.settings["headers"]["Content-Security-Policy"])
98+
return cast("str", self.settings["headers"]["Content-Security-Policy"])
9999

100100
return "; ".join(
101101
[
@@ -173,7 +173,7 @@ def get_current_user(self) -> str:
173173
DeprecationWarning,
174174
stacklevel=2,
175175
)
176-
return cast(str, self._jupyter_current_user)
176+
return cast("str", self._jupyter_current_user)
177177
# haven't called get_user in prepare, raise
178178
raise RuntimeError(msg)
179179

@@ -224,7 +224,7 @@ def login_available(self) -> bool:
224224
whether the user is already logged in or not.
225225
226226
"""
227-
return cast(bool, self.identity_provider.login_available)
227+
return cast("bool", self.identity_provider.login_available)
228228

229229
@property
230230
def authorizer(self) -> Authorizer:
@@ -302,34 +302,34 @@ def serverapp(self) -> ServerApp | None:
302302
@property
303303
def version_hash(self) -> str:
304304
"""The version hash to use for cache hints for static files"""
305-
return cast(str, self.settings.get("version_hash", ""))
305+
return cast("str", self.settings.get("version_hash", ""))
306306

307307
@property
308308
def mathjax_url(self) -> str:
309-
url = cast(str, self.settings.get("mathjax_url", ""))
309+
url = cast("str", self.settings.get("mathjax_url", ""))
310310
if not url or url_is_absolute(url):
311311
return url
312312
return url_path_join(self.base_url, url)
313313

314314
@property
315315
def mathjax_config(self) -> str:
316-
return cast(str, self.settings.get("mathjax_config", "TeX-AMS-MML_HTMLorMML-full,Safe"))
316+
return cast("str", self.settings.get("mathjax_config", "TeX-AMS-MML_HTMLorMML-full,Safe"))
317317

318318
@property
319319
def default_url(self) -> str:
320-
return cast(str, self.settings.get("default_url", ""))
320+
return cast("str", self.settings.get("default_url", ""))
321321

322322
@property
323323
def ws_url(self) -> str:
324-
return cast(str, self.settings.get("websocket_url", ""))
324+
return cast("str", self.settings.get("websocket_url", ""))
325325

326326
@property
327327
def contents_js_source(self) -> str:
328328
self.log.debug(
329329
"Using contents: %s",
330330
self.settings.get("contents_js_source", "services/contents"),
331331
)
332-
return cast(str, self.settings.get("contents_js_source", "services/contents"))
332+
return cast("str", self.settings.get("contents_js_source", "services/contents"))
333333

334334
# ---------------------------------------------------------------
335335
# Manager objects
@@ -370,7 +370,7 @@ def event_logger(self) -> EventLogger:
370370
@property
371371
def allow_origin(self) -> str:
372372
"""Normal Access-Control-Allow-Origin"""
373-
return cast(str, self.settings.get("allow_origin", ""))
373+
return cast("str", self.settings.get("allow_origin", ""))
374374

375375
@property
376376
def allow_origin_pat(self) -> str | None:
@@ -380,7 +380,7 @@ def allow_origin_pat(self) -> str | None:
380380
@property
381381
def allow_credentials(self) -> bool:
382382
"""Whether to set Access-Control-Allow-Credentials"""
383-
return cast(bool, self.settings.get("allow_credentials", False))
383+
return cast("bool", self.settings.get("allow_credentials", False))
384384

385385
def set_default_headers(self) -> None:
386386
"""Add CORS headers, if defined"""

jupyter_server/extension/handler.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def get_template(self, name: str) -> Template:
2626
"""Return the jinja template object for a given name"""
2727
try:
2828
env = f"{self.name}_jinja2_env" # type:ignore[attr-defined]
29-
template = cast(Template, self.settings[env].get_template(name)) # type:ignore[attr-defined]
29+
template = cast("Template", self.settings[env].get_template(name)) # type:ignore[attr-defined]
3030
return template
3131
except TemplateNotFound:
32-
return cast(Template, super().get_template(name)) # type:ignore[misc]
32+
return cast("Template", super().get_template(name)) # type:ignore[misc]
3333

3434

3535
class ExtensionHandlerMixin:
@@ -64,12 +64,12 @@ def serverapp(self) -> ServerApp:
6464
@property
6565
def log(self) -> Logger:
6666
if not hasattr(self, "name"):
67-
return cast(Logger, super().log) # type:ignore[misc]
67+
return cast("Logger", super().log) # type:ignore[misc]
6868
# Attempt to pull the ExtensionApp's log, otherwise fall back to ServerApp.
6969
try:
70-
return cast(Logger, self.extensionapp.log)
70+
return cast("Logger", self.extensionapp.log)
7171
except AttributeError:
72-
return cast(Logger, self.serverapp.log)
72+
return cast("Logger", self.serverapp.log)
7373

7474
@property
7575
def config(self) -> Config:
@@ -81,7 +81,7 @@ def server_config(self) -> Config:
8181

8282
@property
8383
def base_url(self) -> str:
84-
return cast(str, self.settings.get("base_url", "/"))
84+
return cast("str", self.settings.get("base_url", "/"))
8585

8686
def render_template(self, name: str, **ns) -> str:
8787
"""Override render template to handle static_paths
@@ -90,20 +90,20 @@ def render_template(self, name: str, **ns) -> str:
9090
(e.g. default error pages)
9191
make sure our extension-specific static_url is _not_ used.
9292
"""
93-
template = cast(Template, self.get_template(name)) # type:ignore[attr-defined]
93+
template = cast("Template", self.get_template(name)) # type:ignore[attr-defined]
9494
ns.update(self.template_namespace) # type:ignore[attr-defined]
9595
if template.environment is self.settings["jinja2_env"]:
9696
# default template environment, use default static_url
9797
ns["static_url"] = super().static_url # type:ignore[misc]
98-
return cast(str, template.render(**ns))
98+
return cast("str", template.render(**ns))
9999

100100
@property
101101
def static_url_prefix(self) -> str:
102102
return self.extensionapp.static_url_prefix
103103

104104
@property
105105
def static_path(self) -> str:
106-
return cast(str, self.settings[f"{self.name}_static_paths"])
106+
return cast("str", self.settings[f"{self.name}_static_paths"])
107107

108108
def static_url(self, path: str, include_host: bool | None = None, **kwargs: Any) -> str:
109109
"""Returns a static URL for the given relative static file path.
@@ -151,4 +151,4 @@ def static_url(self, path: str, include_host: bool | None = None, **kwargs: Any)
151151
"static_url_prefix": self.static_url_prefix,
152152
}
153153

154-
return base + cast(str, get_url(settings, path, **kwargs))
154+
return base + cast("str", get_url(settings, path, **kwargs))

jupyter_server/gateway/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def stop(self) -> None:
674674
msgs.append(msg["msg_type"])
675675
if self.channel_name == "iopub" and "shutdown_reply" in msgs:
676676
return
677-
if len(msgs):
677+
if msgs:
678678
self.log.warning(
679679
f"Stopping channel '{self.channel_name}' with {len(msgs)} unprocessed non-status messages: {msgs}."
680680
)

jupyter_server/prometheus/metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
__all__ = [
7474
"HTTP_REQUEST_DURATION_SECONDS",
75-
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
7675
"KERNEL_CURRENTLY_RUNNING_TOTAL",
7776
"SERVER_INFO",
77+
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
7878
]

jupyter_server/serverapp.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def _default_ip(self) -> str:
10371037

10381038
@validate("ip")
10391039
def _validate_ip(self, proposal: t.Any) -> str:
1040-
value = t.cast(str, proposal["value"])
1040+
value = t.cast("str", proposal["value"])
10411041
if value == "*":
10421042
value = ""
10431043
return value
@@ -1539,7 +1539,7 @@ def _deprecated_cookie_config(self, change: t.Any) -> None:
15391539

15401540
@validate("base_url")
15411541
def _update_base_url(self, proposal: t.Any) -> str:
1542-
value = t.cast(str, proposal["value"])
1542+
value = t.cast("str", proposal["value"])
15431543
if not value.startswith("/"):
15441544
value = "/" + value
15451545
if not value.endswith("/"):
@@ -2335,8 +2335,7 @@ def init_resources(self) -> None:
23352335
soft = self.min_open_files_limit
23362336
hard = old_hard
23372337
if soft is not None and old_soft < soft:
2338-
if hard < soft:
2339-
hard = soft
2338+
hard = max(hard, soft)
23402339
self.log.debug(
23412340
f"Raising open file limit: soft {old_soft}->{soft}; hard {old_hard}->{hard}"
23422341
)
@@ -2870,7 +2869,7 @@ async def cleanup_extensions(self) -> None:
28702869

28712870
def running_server_info(self, kernel_count: bool = True) -> str:
28722871
"""Return the current working directory and the server url information"""
2873-
info = t.cast(str, self.contents_manager.info_string()) + "\n"
2872+
info = t.cast("str", self.contents_manager.info_string()) + "\n"
28742873
if kernel_count:
28752874
n_kernels = len(self.kernel_manager.list_kernel_ids())
28762875
kernel_msg = trans.ngettext("%d active kernel", "%d active kernels", n_kernels)

jupyter_server/services/api/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def get(self):
120120
@web.authenticated
121121
async def patch(self):
122122
"""Update user information."""
123-
user_data = cast(dict[UpdatableField, str], self.get_json_body())
123+
user_data = cast("dict[UpdatableField, str]", self.get_json_body())
124124
if not user_data:
125125
raise web.HTTPError(400, "Invalid or missing JSON body")
126126

jupyter_server/services/events/handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ def validate_model(
8181
if key not in data:
8282
message = f"Missing `{key}` in the JSON request body."
8383
raise Exception(message)
84-
schema_id = cast(str, data.get("schema_id"))
84+
schema_id = cast("str", data.get("schema_id"))
8585
# The case where a given schema_id isn't found,
8686
# jupyter_events raises a useful error, so there's no need to
8787
# handle that case here.
8888
schema = registry.get(schema_id)
89-
version = str(cast(str, data.get("version")))
89+
version = str(cast("str", data.get("version")))
9090
if schema.version != version:
9191
message = f"Unregistered version: {version!r}{schema.version!r} for `{schema_id}`"
9292
raise Exception(message)
@@ -126,7 +126,7 @@ async def post(self):
126126
try:
127127
validate_model(payload, self.event_logger.schemas)
128128
self.event_logger.emit(
129-
schema_id=cast(str, payload.get("schema_id")),
129+
schema_id=cast("str", payload.get("schema_id")),
130130
data=cast("dict[str, Any]", payload.get("data")),
131131
timestamp_override=get_timestamp(payload),
132132
)

0 commit comments

Comments
 (0)