Skip to content

Commit

Permalink
Clean up lint and typing (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Nov 2, 2023
1 parent a5c288c commit b882a32
Show file tree
Hide file tree
Showing 35 changed files with 88 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- name: Run Linters
run: |
hatch run typing:test
hatch run lint:style
hatch run lint:build
pipx run interrogate -v .
pipx run doc8 --max-line-length=200 --ignore-path=docs/source/other/full-config.rst
npm install -g eslint
Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ repos:
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.6.1"
hooks:
- id: mypy
files: jupyter_server
stages: [manual]
additional_dependencies:
["traitlets>=5.13", "jupyter_core>=5.5", "jupyter_client>=8.5"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
hooks:
Expand Down
3 changes: 1 addition & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ Some of the hooks only run on CI by default, but you can invoke them by
running with the ``--hook-stage manual`` argument.

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

Troubleshooting the Installation
Expand Down
2 changes: 1 addition & 1 deletion examples/identity/system_password/jupyter_server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pwd
from getpass import getuser

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

from jupyter_server.auth.identity import IdentityProvider, User

Expand Down
19 changes: 12 additions & 7 deletions jupyter_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
JUPYTER_SERVER_EVENTS_URI = "https://events.jupyter.org/jupyter_server"
DEFAULT_EVENTS_SCHEMA_PATH = pathlib.Path(__file__).parent / "event_schemas"

del os
from ._version import __version__, version_info
from .base.call_context import CallContext

from ._version import __version__, version_info # noqa
from .base.call_context import CallContext # noqa


def _cleanup():
pass
__all__ = [
"DEFAULT_STATIC_FILES_PATH",
"DEFAULT_TEMPLATE_PATH_LIST",
"DEFAULT_JUPYTER_SERVER_PORT",
"JUPYTER_SERVER_EVENTS_URI",
"DEFAULT_EVENTS_SCHEMA_PATH",
"__version__",
"version_info",
"CallContext",
]
8 changes: 4 additions & 4 deletions jupyter_server/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .authorizer import * # noqa
from .decorator import authorized # noqa
from .identity import * # noqa
from .security import passwd # noqa
from .authorizer import * # noqa: F403
from .decorator import authorized # noqa: F401
from .identity import * # noqa: F403
from .security import passwd # noqa: F401
2 changes: 1 addition & 1 deletion jupyter_server/auth/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from jupyter_core.paths import jupyter_config_dir
from traitlets.log import get_logger

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


Expand Down
3 changes: 2 additions & 1 deletion jupyter_server/auth/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from contextlib import contextmanager

from jupyter_core.paths import jupyter_config_dir
from traitlets.config import Config, ConfigFileNotFound, JSONFileConfigLoader
from traitlets.config import Config
from traitlets.config.loader import ConfigFileNotFound, JSONFileConfigLoader

# Length of the salt in nr of hex chars, which implies salt_len * 4
# bits of randomness.
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from jupyter_server import CallContext
from jupyter_server._sysinfo import get_sys_info
from jupyter_server._tz import utcnow
from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.i18n import combine_translations
from jupyter_server.services.security import csp_report_uri
from jupyter_server.utils import (
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/files/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.base.handlers import JupyterHandler

AUTH_RESOURCE = "contents"
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/gateway/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from ..services.kernels.connection.base import BaseKernelWebsocketConnection
from ..utils import url_path_join
from .managers import GatewayClient
from .gateway_client import GatewayClient


class GatewayWebSocketConnection(BaseKernelWebsocketConnection):
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/gateway/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from ..base.handlers import APIHandler, JupyterHandler
from ..utils import url_path_join
from .managers import GatewayClient
from .gateway_client import GatewayClient

warnings.warn(
"The jupyter_server.gateway.handlers module is deprecated and will not be supported in Jupyter Server 3.0",
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/gateway/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from time import monotonic
from typing import Any, Dict, Optional

import websocket # type:ignore[import-untyped]
import websocket
from jupyter_client.asynchronous.client import AsyncKernelClient
from jupyter_client.clientabc import KernelClientABC
from jupyter_client.kernelspec import KernelSpecManager
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ..base.handlers import JupyterHandler
from ..services.kernelspecs.handlers import kernel_name_regex
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tornado import web
from tornado.log import app_log

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

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

Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/prometheus/log_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Log functions for prometheus"""
from .metrics import HTTP_REQUEST_DURATION_SECONDS
from .metrics import HTTP_REQUEST_DURATION_SECONDS # type:ignore[unused-ignore]


def prometheus_log_method(handler):
Expand Down
9 changes: 8 additions & 1 deletion jupyter_server/prometheus/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
# Try to de-duplicate by using the ones in Notebook if available.
# See https://github.com/jupyter/jupyter_server/issues/209
from notebook.prometheus.metrics import ( # type:ignore[import-not-found]
from notebook.prometheus.metrics import (
HTTP_REQUEST_DURATION_SECONDS,
KERNEL_CURRENTLY_RUNNING_TOTAL,
TERMINAL_CURRENTLY_RUNNING_TOTAL,
Expand All @@ -34,3 +34,10 @@
"counter for how many kernels are running labeled by type",
["type"],
)


__all__ = [
"HTTP_REQUEST_DURATION_SECONDS",
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
"KERNEL_CURRENTLY_RUNNING_TOTAL",
]
6 changes: 3 additions & 3 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
from jupyter_server.extension.manager import ExtensionManager
from jupyter_server.extension.serverextension import ServerExtensionApp
from jupyter_server.gateway.connections import GatewayWebSocketConnection
from jupyter_server.gateway.gateway_client import GatewayClient
from jupyter_server.gateway.managers import (
GatewayClient,
GatewayKernelSpecManager,
GatewayMappingKernelManager,
GatewaySessionManager,
Expand Down Expand Up @@ -323,7 +323,7 @@ def init_settings(
localedir=os.path.join(base_dir, "jupyter_server/i18n"),
fallback=True,
)
env.install_gettext_translations(nbui, newstyle=False) # type:ignore[attr-defined]
env.install_gettext_translations(nbui, newstyle=False)

if sys_info["commit_source"] == "repository":
# don't cache (rely on 304) when working from master
Expand Down Expand Up @@ -1913,7 +1913,7 @@ def init_configurables(self) -> None:
"connection_dir": self.runtime_dir,
"kernel_spec_manager": self.kernel_spec_manager,
}
if jupyter_client.version_info > (8, 3, 0):
if jupyter_client.version_info > (8, 3, 0): # type:ignore[attr-defined]
if self.allow_external_kernels:
external_connection_dir = self.external_connection_dir
if external_connection_dir is None:
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tornado import web

from jupyter_server._tz import isoformat, utcfromtimestamp
from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ...base.handlers import APIHandler, JupyterHandler

Expand Down
4 changes: 3 additions & 1 deletion jupyter_server/services/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .manager import ConfigManager # noqa
from .manager import ConfigManager

__all__ = ["ConfigManager"]
2 changes: 1 addition & 1 deletion jupyter_server/services/config/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ...base.handlers import APIHandler

Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/contents/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.base.handlers import APIHandler, JupyterHandler, path_regex
from jupyter_server.utils import url_escape, url_path_join

Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import warnings
from fnmatch import fnmatch

from jupyter_client.utils import run_sync
from jupyter_core.utils import ensure_async
from jupyter_core.utils import ensure_async, run_sync
from jupyter_events import EventLogger
from nbformat import ValidationError, sign
from nbformat import validate as validate_nb
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/events/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jupyter_events.logger
from tornado import web, websocket

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.base.handlers import JupyterHandler

from ...base.handlers import APIHandler
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/kernels/connection/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from concurrent.futures import Future
from textwrap import dedent

from jupyter_client import protocol_version as client_protocol_version
from jupyter_client import protocol_version as client_protocol_version # type:ignore[attr-defined]
from tornado import gen, web
from tornado.ioloop import IOLoop
from tornado.websocket import WebSocketClosedError
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/kernels/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.utils import url_escape, url_path_join

from ...base.handlers import APIHandler
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ...base.handlers import APIHandler
from ...utils import url_path_join, url_unescape
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from anyio.to_thread import run_sync
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ...base.handlers import APIHandler

Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/security/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the Modified BSD License.
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ...base.handlers import APIHandler
from . import csp_report_uri
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/sessions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.utils import url_path_join

from ...base.handlers import APIHandler
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/sessions/sessionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sqlite3
except ImportError:
# fallback on pysqlite2 if Python was build without sqlite
from pysqlite2 import dbapi2 as sqlite3 # type:ignore[import-not-found,no-redef]
from pysqlite2 import dbapi2 as sqlite3 # type:ignore[no-redef]

from dataclasses import dataclass, fields

Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
from tornado import ioloop, web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized
from jupyter_server.base.handlers import JupyterHandler

AUTH_RESOURCE = "server"
Expand Down
15 changes: 11 additions & 4 deletions jupyter_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,28 @@
SplitResult,
quote,
unquote,
urljoin, # noqa
urlparse,
urlsplit,
urlunsplit,
)
from urllib.request import pathname2url # noqa: F401
from urllib.parse import (
urljoin as _urljoin,
)
from urllib.request import pathname2url as _pathname2url

from _frozen_importlib_external import _NamespacePath # type:ignore[import-not-found]
from jupyter_core.utils import ensure_async
from _frozen_importlib_external import _NamespacePath
from jupyter_core.utils import ensure_async as _ensure_async
from packaging.version import Version
from tornado.httpclient import AsyncHTTPClient, HTTPClient, HTTPRequest, HTTPResponse
from tornado.netutil import Resolver

ApiPath = NewType("ApiPath", str)

# Re-export
urljoin = _urljoin
pathname2url = _pathname2url
ensure_async = _ensure_async


def url_path_join(*pieces: str) -> str:
"""Join components of url into a relative url
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/view/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jupyter_core.utils import ensure_async
from tornado import web

from jupyter_server.auth import authorized
from jupyter_server.auth.decorator import authorized

from ..base.handlers import JupyterHandler, path_regex
from ..utils import url_escape, url_path_join
Expand Down
Loading

0 comments on commit b882a32

Please sign in to comment.