Skip to content

Commit

Permalink
Update lint deps and add more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Oct 21, 2023
1 parent 515004a commit 9e69d36
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
16 changes: 10 additions & 6 deletions ipykernel/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import json
import sys
from subprocess import PIPE, Popen
from typing import Any, Dict
from typing import TYPE_CHECKING, Any

import jupyter_client
from jupyter_client import write_connection_file

if TYPE_CHECKING:
from ipykernel.kernelapp import IPKernelApp

def get_connection_file(app=None):

def get_connection_file(app: IPKernelApp | None = None) -> str:
"""Return the path to the connection file of an app
Parameters
Expand Down Expand Up @@ -46,7 +50,7 @@ def _find_connection_file(connection_file):
return jupyter_client.find_connection_file(connection_file)


def get_connection_info(connection_file=None, unpack=False):
def get_connection_info(connection_file: str | None = None, unpack: bool = False) -> str:
"""Return the connection information for the current Kernel.
Parameters
Expand Down Expand Up @@ -82,7 +86,7 @@ def get_connection_info(connection_file=None, unpack=False):
return info_str


def connect_qtconsole(connection_file=None, argv=None):
def connect_qtconsole(connection_file: str | None = None, argv: list[str] | None = None) -> Popen:
"""Connect a qtconsole to the current kernel.
This is useful for connecting a second qtconsole to a kernel, or to a
Expand Down Expand Up @@ -111,13 +115,13 @@ def connect_qtconsole(connection_file=None, argv=None):

cmd = ";".join(["from qtconsole import qtconsoleapp", "qtconsoleapp.main()"])

kwargs: Dict[str, Any] = {}
kwargs: dict[str, Any] = {}
# Launch the Qt console in a separate session & process group, so
# interrupting the kernel doesn't kill it.
kwargs["start_new_session"] = True

return Popen(
[sys.executable, "-c", cmd, "--existing", cf, *argv], # noqa
[sys.executable, "-c", cmd, "--existing", cf, *argv], # noqa: S603
stdout=PIPE,
stderr=PIPE,
close_fds=(sys.platform != "win32"),
Expand Down
35 changes: 23 additions & 12 deletions ipykernel/kernelspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import annotations

import errno
import json
import os
import shutil
import stat
import sys
import tempfile
from typing import Any

from jupyter_client.kernelspec import KernelSpecManager
from traitlets import Unicode
Expand All @@ -27,7 +30,11 @@
RESOURCES = pjoin(os.path.dirname(__file__), "resources")


def make_ipkernel_cmd(mod="ipykernel_launcher", executable=None, extra_arguments=None):
def make_ipkernel_cmd(
mod: str = "ipykernel_launcher",
executable: str | None = None,
extra_arguments: list[str] | None = None,
) -> list[str]:
"""Build Popen command list for launching an IPython kernel.
Parameters
Expand All @@ -52,7 +59,7 @@ def make_ipkernel_cmd(mod="ipykernel_launcher", executable=None, extra_arguments
return arguments


def get_kernel_dict(extra_arguments=None):
def get_kernel_dict(extra_arguments: list[str] | None = None) -> dict[str, Any]:
"""Construct dict for kernel.json"""
return {
"argv": make_ipkernel_cmd(extra_arguments=extra_arguments),
Expand All @@ -62,7 +69,11 @@ def get_kernel_dict(extra_arguments=None):
}


def write_kernel_spec(path=None, overrides=None, extra_arguments=None):
def write_kernel_spec(
path: str | None = None,
overrides: dict[str, Any] | None = None,
extra_arguments: list[str] | None = None,
) -> str:
"""Write a kernel spec directory to `path`
If `path` is not specified, a temporary directory is created.
Expand Down Expand Up @@ -93,13 +104,13 @@ def write_kernel_spec(path=None, overrides=None, extra_arguments=None):


def install(
kernel_spec_manager=None,
user=False,
kernel_name=KERNEL_NAME,
display_name=None,
prefix=None,
profile=None,
env=None,
kernel_spec_manager: KernelSpecManager | None = None,
user: bool = False,
kernel_name: str = KERNEL_NAME,
display_name: str | None = None,
prefix: str | None = None,
profile: str | None = None,
env: dict[str, str] | None = None,
):
"""Install the IPython kernelspec for Jupyter
Expand Down Expand Up @@ -167,13 +178,13 @@ class InstallIPythonKernelSpecApp(Application):

name = Unicode("ipython-kernel-install")

def initialize(self, argv=None):
def initialize(self, argv: list[str] | None = None) -> None:
"""Initialize the app."""
if argv is None:
argv = sys.argv[1:]
self.argv = argv

def start(self):
def start(self) -> None:
"""Start the app."""
import argparse

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ matrix.qt.features = [

[tool.hatch.envs.typing]
features = ["test"]
dependencies = ["mypy>=1.5.1", "traitlets>=5.11.2", "ipython>=8.16.1"]
dependencies = ["mypy>=1.6.0", "traitlets>=5.11.2", "ipython>=8.16.1"]
[tool.hatch.envs.typing.scripts]
test = "mypy --install-types --non-interactive {args}"

[tool.hatch.envs.lint]
dependencies = ["black==23.3.0", "mdformat>0.7", "ruff==0.0.287"]
dependencies = ["black==23.3.0", "mdformat>0.7", "ruff==0.1.1"]
detached = true
[tool.hatch.envs.lint.scripts]
style = [
Expand Down

0 comments on commit 9e69d36

Please sign in to comment.