Skip to content

Commit b882a32

Browse files
authored
Clean up lint and typing (jupyter-server#1351)
1 parent a5c288c commit b882a32

35 files changed

+88
-78
lines changed

.github/workflows/python-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
- name: Run Linters
8383
run: |
8484
hatch run typing:test
85-
hatch run lint:style
85+
hatch run lint:build
8686
pipx run interrogate -v .
8787
pipx run doc8 --max-line-length=200 --ignore-path=docs/source/other/full-config.rst
8888
npm install -g eslint

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ repos:
5757
- id: rst-directive-colons
5858
- id: rst-inline-touching-normal
5959

60+
- repo: https://github.com/pre-commit/mirrors-mypy
61+
rev: "v1.6.1"
62+
hooks:
63+
- id: mypy
64+
files: jupyter_server
65+
stages: [manual]
66+
additional_dependencies:
67+
["traitlets>=5.13", "jupyter_core>=5.5", "jupyter_client>=8.5"]
68+
6069
- repo: https://github.com/astral-sh/ruff-pre-commit
6170
rev: v0.1.3
6271
hooks:

CONTRIBUTING.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ Some of the hooks only run on CI by default, but you can invoke them by
7070
running with the ``--hook-stage manual`` argument.
7171

7272
There are three hatch scripts that can be run locally as well:
73-
``hatch run lint:style`` will check styling. ``hatch run lint:fmt``
74-
will attempt to auto-format files. ``hatch run typing:test`` will
73+
``hatch run lint:build`` will enforce styling. ``hatch run typing:test`` will
7574
run the type checker.
7675

7776
Troubleshooting the Installation

examples/identity/system_password/jupyter_server_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pwd
33
from getpass import getuser
44

5-
from pamela import PAMError, authenticate # type:ignore[import-not-found]
5+
from pamela import PAMError, authenticate
66

77
from jupyter_server.auth.identity import IdentityProvider, User
88

jupyter_server/__init__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
JUPYTER_SERVER_EVENTS_URI = "https://events.jupyter.org/jupyter_server"
1313
DEFAULT_EVENTS_SCHEMA_PATH = pathlib.Path(__file__).parent / "event_schemas"
1414

15-
del os
15+
from ._version import __version__, version_info
16+
from .base.call_context import CallContext
1617

17-
from ._version import __version__, version_info # noqa
18-
from .base.call_context import CallContext # noqa
19-
20-
21-
def _cleanup():
22-
pass
18+
__all__ = [
19+
"DEFAULT_STATIC_FILES_PATH",
20+
"DEFAULT_TEMPLATE_PATH_LIST",
21+
"DEFAULT_JUPYTER_SERVER_PORT",
22+
"JUPYTER_SERVER_EVENTS_URI",
23+
"DEFAULT_EVENTS_SCHEMA_PATH",
24+
"__version__",
25+
"version_info",
26+
"CallContext",
27+
]

jupyter_server/auth/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .authorizer import * # noqa
2-
from .decorator import authorized # noqa
3-
from .identity import * # noqa
4-
from .security import passwd # noqa
1+
from .authorizer import * # noqa: F403
2+
from .decorator import authorized # noqa: F401
3+
from .identity import * # noqa: F403
4+
from .security import passwd # noqa: F401

jupyter_server/auth/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from jupyter_core.paths import jupyter_config_dir
88
from traitlets.log import get_logger
99

10-
from jupyter_server.auth import passwd
10+
from jupyter_server.auth import passwd # type:ignore[attr-defined]
1111
from jupyter_server.config_manager import BaseJSONConfigManager
1212

1313

jupyter_server/auth/security.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from contextlib import contextmanager
1212

1313
from jupyter_core.paths import jupyter_config_dir
14-
from traitlets.config import Config, ConfigFileNotFound, JSONFileConfigLoader
14+
from traitlets.config import Config
15+
from traitlets.config.loader import ConfigFileNotFound, JSONFileConfigLoader
1516

1617
# Length of the salt in nr of hex chars, which implies salt_len * 4
1718
# bits of randomness.

jupyter_server/base/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from jupyter_server import CallContext
3131
from jupyter_server._sysinfo import get_sys_info
3232
from jupyter_server._tz import utcnow
33-
from jupyter_server.auth import authorized
33+
from jupyter_server.auth.decorator import authorized
3434
from jupyter_server.i18n import combine_translations
3535
from jupyter_server.services.security import csp_report_uri
3636
from jupyter_server.utils import (

jupyter_server/files/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from jupyter_core.utils import ensure_async
1111
from tornado import web
1212

13-
from jupyter_server.auth import authorized
13+
from jupyter_server.auth.decorator import authorized
1414
from jupyter_server.base.handlers import JupyterHandler
1515

1616
AUTH_RESOURCE = "contents"

jupyter_server/gateway/connections.py

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

1717
from ..services.kernels.connection.base import BaseKernelWebsocketConnection
1818
from ..utils import url_path_join
19-
from .managers import GatewayClient
19+
from .gateway_client import GatewayClient
2020

2121

2222
class GatewayWebSocketConnection(BaseKernelWebsocketConnection):

jupyter_server/gateway/handlers.py

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

2121
from ..base.handlers import APIHandler, JupyterHandler
2222
from ..utils import url_path_join
23-
from .managers import GatewayClient
23+
from .gateway_client import GatewayClient
2424

2525
warnings.warn(
2626
"The jupyter_server.gateway.handlers module is deprecated and will not be supported in Jupyter Server 3.0",

jupyter_server/gateway/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from time import monotonic
1212
from typing import Any, Dict, Optional
1313

14-
import websocket # type:ignore[import-untyped]
14+
import websocket
1515
from jupyter_client.asynchronous.client import AsyncKernelClient
1616
from jupyter_client.clientabc import KernelClientABC
1717
from jupyter_client.kernelspec import KernelSpecManager

jupyter_server/kernelspecs/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from jupyter_core.utils import ensure_async
55
from tornado import web
66

7-
from jupyter_server.auth import authorized
7+
from jupyter_server.auth.decorator import authorized
88

99
from ..base.handlers import JupyterHandler
1010
from ..services.kernelspecs.handlers import kernel_name_regex

jupyter_server/nbconvert/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from tornado import web
1313
from tornado.log import app_log
1414

15-
from jupyter_server.auth import authorized
15+
from jupyter_server.auth.decorator import authorized
1616

1717
from ..base.handlers import FilesRedirectHandler, JupyterHandler, path_regex
1818

jupyter_server/prometheus/log_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Log functions for prometheus"""
2-
from .metrics import HTTP_REQUEST_DURATION_SECONDS
2+
from .metrics import HTTP_REQUEST_DURATION_SECONDS # type:ignore[unused-ignore]
33

44

55
def prometheus_log_method(handler):

jupyter_server/prometheus/metrics.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
1010
# Try to de-duplicate by using the ones in Notebook if available.
1111
# See https://github.com/jupyter/jupyter_server/issues/209
12-
from notebook.prometheus.metrics import ( # type:ignore[import-not-found]
12+
from notebook.prometheus.metrics import (
1313
HTTP_REQUEST_DURATION_SECONDS,
1414
KERNEL_CURRENTLY_RUNNING_TOTAL,
1515
TERMINAL_CURRENTLY_RUNNING_TOTAL,
@@ -34,3 +34,10 @@
3434
"counter for how many kernels are running labeled by type",
3535
["type"],
3636
)
37+
38+
39+
__all__ = [
40+
"HTTP_REQUEST_DURATION_SECONDS",
41+
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
42+
"KERNEL_CURRENTLY_RUNNING_TOTAL",
43+
]

jupyter_server/serverapp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
from jupyter_server.extension.manager import ExtensionManager
9292
from jupyter_server.extension.serverextension import ServerExtensionApp
9393
from jupyter_server.gateway.connections import GatewayWebSocketConnection
94+
from jupyter_server.gateway.gateway_client import GatewayClient
9495
from jupyter_server.gateway.managers import (
95-
GatewayClient,
9696
GatewayKernelSpecManager,
9797
GatewayMappingKernelManager,
9898
GatewaySessionManager,
@@ -323,7 +323,7 @@ def init_settings(
323323
localedir=os.path.join(base_dir, "jupyter_server/i18n"),
324324
fallback=True,
325325
)
326-
env.install_gettext_translations(nbui, newstyle=False) # type:ignore[attr-defined]
326+
env.install_gettext_translations(nbui, newstyle=False)
327327

328328
if sys_info["commit_source"] == "repository":
329329
# don't cache (rely on 304) when working from master
@@ -1913,7 +1913,7 @@ def init_configurables(self) -> None:
19131913
"connection_dir": self.runtime_dir,
19141914
"kernel_spec_manager": self.kernel_spec_manager,
19151915
}
1916-
if jupyter_client.version_info > (8, 3, 0):
1916+
if jupyter_client.version_info > (8, 3, 0): # type:ignore[attr-defined]
19171917
if self.allow_external_kernels:
19181918
external_connection_dir = self.external_connection_dir
19191919
if external_connection_dir is None:

jupyter_server/services/api/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from tornado import web
1010

1111
from jupyter_server._tz import isoformat, utcfromtimestamp
12-
from jupyter_server.auth import authorized
12+
from jupyter_server.auth.decorator import authorized
1313

1414
from ...base.handlers import APIHandler, JupyterHandler
1515

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from .manager import ConfigManager # noqa
1+
from .manager import ConfigManager
2+
3+
__all__ = ["ConfigManager"]

jupyter_server/services/config/handlers.py

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

66
from tornado import web
77

8-
from jupyter_server.auth import authorized
8+
from jupyter_server.auth.decorator import authorized
99

1010
from ...base.handlers import APIHandler
1111

jupyter_server/services/contents/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from jupyter_core.utils import ensure_async
1515
from tornado import web
1616

17-
from jupyter_server.auth import authorized
17+
from jupyter_server.auth.decorator import authorized
1818
from jupyter_server.base.handlers import APIHandler, JupyterHandler, path_regex
1919
from jupyter_server.utils import url_escape, url_path_join
2020

jupyter_server/services/contents/manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import warnings
1212
from fnmatch import fnmatch
1313

14-
from jupyter_client.utils import run_sync
15-
from jupyter_core.utils import ensure_async
14+
from jupyter_core.utils import ensure_async, run_sync
1615
from jupyter_events import EventLogger
1716
from nbformat import ValidationError, sign
1817
from nbformat import validate as validate_nb

jupyter_server/services/events/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import jupyter_events.logger
1010
from tornado import web, websocket
1111

12-
from jupyter_server.auth import authorized
12+
from jupyter_server.auth.decorator import authorized
1313
from jupyter_server.base.handlers import JupyterHandler
1414

1515
from ...base.handlers import APIHandler

jupyter_server/services/kernels/connection/channels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from concurrent.futures import Future
1010
from textwrap import dedent
1111

12-
from jupyter_client import protocol_version as client_protocol_version
12+
from jupyter_client import protocol_version as client_protocol_version # type:ignore[attr-defined]
1313
from tornado import gen, web
1414
from tornado.ioloop import IOLoop
1515
from tornado.websocket import WebSocketClosedError

jupyter_server/services/kernels/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from jupyter_core.utils import ensure_async
1616
from tornado import web
1717

18-
from jupyter_server.auth import authorized
18+
from jupyter_server.auth.decorator import authorized
1919
from jupyter_server.utils import url_escape, url_path_join
2020

2121
from ...base.handlers import APIHandler

jupyter_server/services/kernelspecs/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from jupyter_core.utils import ensure_async
1717
from tornado import web
1818

19-
from jupyter_server.auth import authorized
19+
from jupyter_server.auth.decorator import authorized
2020

2121
from ...base.handlers import APIHandler
2222
from ...utils import url_path_join, url_unescape

jupyter_server/services/nbconvert/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from anyio.to_thread import run_sync
66
from tornado import web
77

8-
from jupyter_server.auth import authorized
8+
from jupyter_server.auth.decorator import authorized
99

1010
from ...base.handlers import APIHandler
1111

jupyter_server/services/security/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Distributed under the terms of the Modified BSD License.
44
from tornado import web
55

6-
from jupyter_server.auth import authorized
6+
from jupyter_server.auth.decorator import authorized
77

88
from ...base.handlers import APIHandler
99
from . import csp_report_uri

jupyter_server/services/sessions/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from jupyter_core.utils import ensure_async
1717
from tornado import web
1818

19-
from jupyter_server.auth import authorized
19+
from jupyter_server.auth.decorator import authorized
2020
from jupyter_server.utils import url_path_join
2121

2222
from ...base.handlers import APIHandler

jupyter_server/services/sessions/sessionmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import sqlite3
1515
except ImportError:
1616
# fallback on pysqlite2 if Python was build without sqlite
17-
from pysqlite2 import dbapi2 as sqlite3 # type:ignore[import-not-found,no-redef]
17+
from pysqlite2 import dbapi2 as sqlite3 # type:ignore[no-redef]
1818

1919
from dataclasses import dataclass, fields
2020

jupyter_server/services/shutdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
from tornado import ioloop, web
44

5-
from jupyter_server.auth import authorized
5+
from jupyter_server.auth.decorator import authorized
66
from jupyter_server.base.handlers import JupyterHandler
77

88
AUTH_RESOURCE = "server"

jupyter_server/utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,28 @@
1515
SplitResult,
1616
quote,
1717
unquote,
18-
urljoin, # noqa
1918
urlparse,
2019
urlsplit,
2120
urlunsplit,
2221
)
23-
from urllib.request import pathname2url # noqa: F401
22+
from urllib.parse import (
23+
urljoin as _urljoin,
24+
)
25+
from urllib.request import pathname2url as _pathname2url
2426

25-
from _frozen_importlib_external import _NamespacePath # type:ignore[import-not-found]
26-
from jupyter_core.utils import ensure_async
27+
from _frozen_importlib_external import _NamespacePath
28+
from jupyter_core.utils import ensure_async as _ensure_async
2729
from packaging.version import Version
2830
from tornado.httpclient import AsyncHTTPClient, HTTPClient, HTTPRequest, HTTPResponse
2931
from tornado.netutil import Resolver
3032

3133
ApiPath = NewType("ApiPath", str)
3234

35+
# Re-export
36+
urljoin = _urljoin
37+
pathname2url = _pathname2url
38+
ensure_async = _ensure_async
39+
3340

3441
def url_path_join(*pieces: str) -> str:
3542
"""Join components of url into a relative url

jupyter_server/view/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from jupyter_core.utils import ensure_async
55
from tornado import web
66

7-
from jupyter_server.auth import authorized
7+
from jupyter_server.auth.decorator import authorized
88

99
from ..base.handlers import JupyterHandler, path_regex
1010
from ..utils import url_escape, url_path_join

0 commit comments

Comments
 (0)