Skip to content

Commit 22b2d21

Browse files
authored
Clean up typing config (#1163)
1 parent 3c4b5bf commit 22b2d21

13 files changed

+41
-47
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
- name: Run Linters
8585
run: |
8686
hatch run typing:test
87-
hatch run lint:style
87+
hatch run lint:build
8888
pipx run interrogate -vv .
8989
pipx run doc8 --max-line-length=200
9090

.pre-commit-config.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ repos:
3939
- id: prettier
4040
types_or: [yaml, html, json]
4141

42+
- repo: https://github.com/pre-commit/mirrors-mypy
43+
rev: "v1.6.1"
44+
hooks:
45+
- id: mypy
46+
files: ipykernel
47+
stages: [manual]
48+
args: ["--install-types", "--non-interactive"]
49+
additional_dependencies:
50+
[
51+
"traitlets>=5.13",
52+
"ipython>=8.16.1",
53+
"jupyter_client>=8.5",
54+
"appnope",
55+
]
56+
4257
- repo: https://github.com/adamchainz/blacken-docs
4358
rev: "1.16.0"
4459
hooks:

ipykernel/comm/comm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
# this is the class that will be created if we do comm.create_comm
19-
class BaseComm(comm.base_comm.BaseComm):
19+
class BaseComm(comm.base_comm.BaseComm): # type:ignore[misc]
2020
"""The base class for comms."""
2121

2222
kernel: Optional["Kernel"] = None

ipykernel/comm/manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
logger = logging.getLogger("ipykernel.comm")
1515

1616

17-
class CommManager(comm.base_comm.CommManager, traitlets.config.LoggingConfigurable):
17+
class CommManager(comm.base_comm.CommManager, traitlets.config.LoggingConfigurable): # type:ignore[misc]
1818
"""A comm manager."""
1919

2020
kernel = traitlets.Instance("ipykernel.kernelbase.Kernel")

ipykernel/connect.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def get_connection_info(
8888
return info_str
8989

9090

91-
def connect_qtconsole(connection_file: str | None = None, argv: list[str] | None = None) -> Popen:
91+
def connect_qtconsole(
92+
connection_file: str | None = None, argv: list[str] | None = None
93+
) -> Popen[Any]:
9294
"""Connect a qtconsole to the current kernel.
9395
9496
This is useful for connecting a second qtconsole to a kernel, or to a

ipykernel/eventloops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def wake():
168168

169169
# We have to put the wx.Timer in a wx.Frame for it to fire properly.
170170
# We make the Frame hidden when we create it in the main app below.
171-
class TimerFrame(wx.Frame):
171+
class TimerFrame(wx.Frame): # type:ignore[misc]
172172
def __init__(self, func):
173173
wx.Frame.__init__(self, None, -1)
174174
self.timer = wx.Timer(self)
@@ -182,7 +182,7 @@ def on_timer(self, event):
182182

183183
# We need a custom wx.App to create our Frame subclass that has the
184184
# wx.Timer to defer back to the tornado event loop.
185-
class IPWxApp(wx.App):
185+
class IPWxApp(wx.App): # type:ignore[misc]
186186
def OnInit(self):
187187
self.frame = TimerFrame(wake)
188188
self.frame.Show(False)

ipykernel/iostream.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(self, socket, pipe=False):
6969
self._event_pipes: Dict[threading.Thread, Any] = {}
7070
self._event_pipe_gc_lock: threading.Lock = threading.Lock()
7171
self._event_pipe_gc_seconds: float = 10
72-
self._event_pipe_gc_task: Optional[asyncio.Task] = None
72+
self._event_pipe_gc_task: Optional[asyncio.Task[Any]] = None
7373
self._setup_event_pipe()
7474
self.thread = threading.Thread(target=self._thread_main, name="IOPub")
7575
self.thread.daemon = True

ipykernel/ipkernel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(self, **kwargs):
147147

148148
if _use_appnope() and self._darwin_app_nap:
149149
# Disable app-nap as the kernel is not a gui but can have guis
150-
import appnope
150+
import appnope # type:ignore[import-untyped]
151151

152152
appnope.nope()
153153

ipykernel/kernelapp.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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 atexit
78
import errno
@@ -10,10 +11,10 @@
1011
import signal
1112
import sys
1213
import traceback
14+
import typing as t
1315
from functools import partial
1416
from io import FileIO, TextIOWrapper
1517
from logging import StreamHandler
16-
from typing import Optional
1718

1819
import zmq
1920
from IPython.core.application import ( # type:ignore[attr-defined]
@@ -132,7 +133,7 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp, ConnectionFileMix
132133
poller = Any() # don't restrict this even though current pollers are all Threads
133134
heartbeat = Instance(Heartbeat, allow_none=True)
134135

135-
context: Optional[zmq.Context] = Any() # type:ignore[assignment]
136+
context: zmq.Context[t.Any] | None = Any() # type:ignore[assignment]
136137
shell_socket = Any()
137138
control_socket = Any()
138139
debugpy_socket = Any()

ipykernel/kernelbase.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,8 @@ async def usage_request(self, stream, ident, parent):
10441044
# Ensure 1) self.processes is updated to only current subprocesses
10451045
# and 2) we reuse processes when possible (needed for accurate CPU)
10461046
self.processes = {
1047-
process.pid: self.processes.get(process.pid, process) for process in all_processes
1047+
process.pid: self.processes.get(process.pid, process) # type:ignore[misc,call-overload]
1048+
for process in all_processes
10481049
}
10491050
reply_content["kernel_cpu"] = sum(
10501051
[
@@ -1062,7 +1063,7 @@ async def usage_request(self, stream, ident, parent):
10621063
cpu_percent = psutil.cpu_percent()
10631064
# https://psutil.readthedocs.io/en/latest/index.html?highlight=cpu#psutil.cpu_percent
10641065
# The first time cpu_percent is called it will return a meaningless 0.0 value which you are supposed to ignore.
1065-
if cpu_percent is not None and cpu_percent != 0.0:
1066+
if cpu_percent is not None and cpu_percent != 0.0: # type:ignore[redundant-expr]
10661067
reply_content["host_cpu_percent"] = cpu_percent
10671068
reply_content["cpu_count"] = psutil.cpu_count(logical=True)
10681069
reply_content["host_virtual_memory"] = dict(psutil.virtual_memory()._asdict())

ipykernel/pylab/backend_inline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import warnings
77

8-
from matplotlib_inline.backend_inline import * # analysis: ignore # noqa F401
8+
from matplotlib_inline.backend_inline import * # type:ignore[import-untyped] # analysis: ignore # noqa F401
99

1010
warnings.warn(
1111
"`ipykernel.pylab.backend_inline` is deprecated, directly "

ipykernel/pylab/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import warnings
77

8-
from matplotlib_inline.config import * # analysis: ignore # noqa F401
8+
from matplotlib_inline.config import * # type:ignore[import-untyped] # analysis: ignore # noqa F401
99

1010
warnings.warn(
1111
"`ipykernel.pylab.config` is deprecated, directly use `matplotlib_inline.config`",

pyproject.toml

+8-33
Original file line numberDiff line numberDiff line change
@@ -111,51 +111,26 @@ matrix.qt.features = [
111111
]
112112

113113
[tool.hatch.envs.typing]
114-
features = ["test"]
115-
dependencies = ["mypy>=1.6.0", "traitlets>=5.13.0", "ipython>=8.16.1", "jupyter_client>=8.5"]
114+
dependencies = ["pre-commit"]
115+
detached = true
116116
[tool.hatch.envs.typing.scripts]
117-
test = "mypy --install-types --non-interactive {args}"
117+
test = "pre-commit run --all-files --hook-stage manual mypy"
118118

119119
[tool.hatch.envs.lint]
120-
dependencies = ["mdformat>0.7", "ruff==0.1.3"]
120+
dependencies = ["pre-commit"]
121121
detached = true
122122
[tool.hatch.envs.lint.scripts]
123-
style = [
124-
"ruff {args:.}",
125-
"ruff format {args:.}",
126-
"mdformat --check {args:docs *.md}"
127-
]
128-
fmt = [
129-
"ruff --fix {args:.}",
130-
"ruff format {args:.}",
131-
"mdformat {args:docs *.md}"
132-
]
123+
build = ["pre-commit run --all-files ruff"]
133124

134125
[tool.mypy]
135126
files = "ipykernel"
136-
check_untyped_defs = true
137-
disallow_incomplete_defs = true
138-
disallow_untyped_decorators = true
127+
strict = true
128+
disable_error_code = ["no-untyped-def", "no-untyped-call", "import-not-found"]
139129
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
140130
follow_imports = "normal"
141-
ignore_missing_imports = true
142-
no_implicit_optional = true
143-
no_implicit_reexport = true
144131
pretty = true
145-
show_error_context = true
146132
show_error_codes = true
147-
strict_equality = true
148-
strict_optional = true
149-
warn_unused_configs = true
150-
warn_redundant_casts = true
151-
warn_return_any = true
152133
warn_unreachable = true
153-
warn_unused_ignores = true
154-
155-
[[tool.mypy.overrides]]
156-
module = "tests.*"
157-
disable_error_code = ["ignore-without-code"]
158-
warn_unreachable = false
159134

160135
[tool.pytest.ini_options]
161136
minversion = "6.0"
@@ -340,4 +315,4 @@ toplevel = ["ipykernel/", "ipykernel_launcher.py"]
340315
ignore = ["W002"]
341316

342317
[tool.repo-review]
343-
ignore = ["PY007", "PP308", "GH102", "PC140", "MY101"]
318+
ignore = ["PY007", "PP308", "GH102", "MY101"]

0 commit comments

Comments
 (0)