Skip to content

Commit 966e0a4

Browse files
authored
Update lint deps and add more typing (#1156)
1 parent 515004a commit 966e0a4

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

ipykernel/connect.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
"""
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
5+
from __future__ import annotations
56

67
import json
78
import sys
89
from subprocess import PIPE, Popen
9-
from typing import Any, Dict
10+
from typing import TYPE_CHECKING, Any
1011

1112
import jupyter_client
1213
from jupyter_client import write_connection_file
1314

15+
if TYPE_CHECKING:
16+
from ipykernel.kernelapp import IPKernelApp
1417

15-
def get_connection_file(app=None):
18+
19+
def get_connection_file(app: IPKernelApp | None = None) -> str:
1620
"""Return the path to the connection file of an app
1721
1822
Parameters
@@ -46,7 +50,9 @@ def _find_connection_file(connection_file):
4650
return jupyter_client.find_connection_file(connection_file)
4751

4852

49-
def get_connection_info(connection_file=None, unpack=False):
53+
def get_connection_info(
54+
connection_file: str | None = None, unpack: bool = False
55+
) -> str | dict[str, Any]:
5056
"""Return the connection information for the current Kernel.
5157
5258
Parameters
@@ -77,12 +83,12 @@ def get_connection_info(connection_file=None, unpack=False):
7783
info = json.loads(info_str)
7884
# ensure key is bytes:
7985
info["key"] = info.get("key", "").encode()
80-
return info
86+
return info # type:ignore[no-any-return]
8187

8288
return info_str
8389

8490

85-
def connect_qtconsole(connection_file=None, argv=None):
91+
def connect_qtconsole(connection_file: str | None = None, argv: list[str] | None = None) -> Popen:
8692
"""Connect a qtconsole to the current kernel.
8793
8894
This is useful for connecting a second qtconsole to a kernel, or to a
@@ -111,13 +117,13 @@ def connect_qtconsole(connection_file=None, argv=None):
111117

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

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

119125
return Popen(
120-
[sys.executable, "-c", cmd, "--existing", cf, *argv], # noqa
126+
[sys.executable, "-c", cmd, "--existing", cf, *argv], # noqa: S603
121127
stdout=PIPE,
122128
stderr=PIPE,
123129
close_fds=(sys.platform != "win32"),

ipykernel/kernelapp.py

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ def write_connection_file(self):
275275
# original file had port number 0, we update with the actual port
276276
# used.
277277
existing_connection_info = get_connection_info(cf, unpack=True)
278+
assert isinstance(existing_connection_info, dict)
278279
connection_info = dict(existing_connection_info, **connection_info)
279280
if connection_info == existing_connection_info:
280281
self.log.debug("Connection file %s with current information already exists", cf)

ipykernel/kernelspec.py

+26-15
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6+
from __future__ import annotations
7+
68
import errno
79
import json
810
import os
911
import shutil
1012
import stat
1113
import sys
1214
import tempfile
15+
from typing import Any
1316

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

2932

30-
def make_ipkernel_cmd(mod="ipykernel_launcher", executable=None, extra_arguments=None):
33+
def make_ipkernel_cmd(
34+
mod: str = "ipykernel_launcher",
35+
executable: str | None = None,
36+
extra_arguments: list[str] | None = None,
37+
) -> list[str]:
3138
"""Build Popen command list for launching an IPython kernel.
3239
3340
Parameters
@@ -52,7 +59,7 @@ def make_ipkernel_cmd(mod="ipykernel_launcher", executable=None, extra_arguments
5259
return arguments
5360

5461

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

6471

65-
def write_kernel_spec(path=None, overrides=None, extra_arguments=None):
72+
def write_kernel_spec(
73+
path: str | None = None,
74+
overrides: dict[str, Any] | None = None,
75+
extra_arguments: list[str] | None = None,
76+
) -> str:
6677
"""Write a kernel spec directory to `path`
6778
6879
If `path` is not specified, a temporary directory is created.
@@ -93,14 +104,14 @@ def write_kernel_spec(path=None, overrides=None, extra_arguments=None):
93104

94105

95106
def install(
96-
kernel_spec_manager=None,
97-
user=False,
98-
kernel_name=KERNEL_NAME,
99-
display_name=None,
100-
prefix=None,
101-
profile=None,
102-
env=None,
103-
):
107+
kernel_spec_manager: KernelSpecManager | None = None,
108+
user: bool = False,
109+
kernel_name: str = KERNEL_NAME,
110+
display_name: str | None = None,
111+
prefix: str | None = None,
112+
profile: str | None = None,
113+
env: dict[str, str] | None = None,
114+
) -> str:
104115
"""Install the IPython kernelspec for Jupyter
105116
106117
Parameters
@@ -136,7 +147,7 @@ def install(
136147
# kernel_name is specified and display_name is not
137148
# default display_name to kernel_name
138149
display_name = kernel_name
139-
overrides = {}
150+
overrides: dict[str, Any] = {}
140151
if display_name:
141152
overrides["display_name"] = display_name
142153
if profile:
@@ -154,7 +165,7 @@ def install(
154165
)
155166
# cleanup afterward
156167
shutil.rmtree(path)
157-
return dest
168+
return dest # type:ignore[no-any-return]
158169

159170

160171
# Entrypoint
@@ -167,13 +178,13 @@ class InstallIPythonKernelSpecApp(Application):
167178

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

170-
def initialize(self, argv=None):
181+
def initialize(self, argv: list[str] | None = None) -> None:
171182
"""Initialize the app."""
172183
if argv is None:
173184
argv = sys.argv[1:]
174185
self.argv = argv
175186

176-
def start(self):
187+
def start(self) -> None:
177188
"""Start the app."""
178189
import argparse
179190

ipykernel/zmqshell.py

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def connect_info(self, arg_s):
378378
if jupyter_runtime_dir() == os.path.dirname(connection_file):
379379
connection_file = os.path.basename(connection_file)
380380

381+
assert isinstance(info, str)
381382
print(info + "\n")
382383
print(
383384
f"Paste the above JSON into a file, and connect with:\n"

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ matrix.qt.features = [
112112

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

119119
[tool.hatch.envs.lint]
120-
dependencies = ["black==23.3.0", "mdformat>0.7", "ruff==0.0.287"]
120+
dependencies = ["black==23.3.0", "mdformat>0.7", "ruff==0.1.1"]
121121
detached = true
122122
[tool.hatch.envs.lint.scripts]
123123
style = [

0 commit comments

Comments
 (0)