Skip to content

Commit

Permalink
Update typings for traitlets 5.10.1 (#974)
Browse files Browse the repository at this point in the history
* Update typings for traitlets 5.10.1

* fixup

* fixup

* typing

* typing

* add workaround for docs

* lint
  • Loading branch information
blink1073 authored Oct 2, 2023
1 parent 60c1095 commit 6ea7118
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 69 deletions.
13 changes: 13 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import logging as pylogging
import os
import os.path as osp
import shutil

from sphinx.util import logging # type:ignore[import]

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
Expand All @@ -37,6 +40,16 @@
"sphinx_autodoc_typehints",
]


# Workaround for https://github.com/agronholm/sphinx-autodoc-typehints/issues/123
class FilterForIssue123(pylogging.Filter):
def filter(self, record: pylogging.LogRecord) -> bool:
return not record.getMessage().startswith("Cannot handle as a local function")


logging.getLogger("sphinx_autodoc_typehints").logger.addFilter(FilterForIssue123())
# End of a workaround

try:
import enchant # type:ignore[import] # noqa

Expand Down
26 changes: 20 additions & 6 deletions jupyter_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def _context_default(self) -> zmq.Context:

def __del__(self):
"""Handle garbage collection. Destroy context if applicable."""
if self._created_context and self.context and not self.context.closed:
if (
self._created_context
and self.context is not None # type:ignore[redundant-expr]
and not self.context.closed
):
if self.channels_running:
if self.log:
self.log.warning("Could not destroy zmq context for %s", self)
Expand Down Expand Up @@ -349,7 +353,9 @@ def shell_channel(self) -> t.Any:
url = self._make_url("shell")
self.log.debug("connecting shell channel to %s", url)
socket = self.connect_shell(identity=self.session.bsession)
self._shell_channel = self.shell_channel_class(socket, self.session, self.ioloop)
self._shell_channel = self.shell_channel_class(
socket, self.session, self.ioloop
) # type:ignore[operator]
return self._shell_channel

@property
Expand All @@ -359,7 +365,9 @@ def iopub_channel(self) -> t.Any:
url = self._make_url("iopub")
self.log.debug("connecting iopub channel to %s", url)
socket = self.connect_iopub()
self._iopub_channel = self.iopub_channel_class(socket, self.session, self.ioloop)
self._iopub_channel = self.iopub_channel_class(
socket, self.session, self.ioloop
) # type:ignore[operator]
return self._iopub_channel

@property
Expand All @@ -369,7 +377,9 @@ def stdin_channel(self) -> t.Any:
url = self._make_url("stdin")
self.log.debug("connecting stdin channel to %s", url)
socket = self.connect_stdin(identity=self.session.bsession)
self._stdin_channel = self.stdin_channel_class(socket, self.session, self.ioloop)
self._stdin_channel = self.stdin_channel_class(
socket, self.session, self.ioloop
) # type:ignore[operator]
return self._stdin_channel

@property
Expand All @@ -378,7 +388,9 @@ def hb_channel(self) -> t.Any:
if self._hb_channel is None:
url = self._make_url("hb")
self.log.debug("connecting heartbeat channel to %s", url)
self._hb_channel = self.hb_channel_class(self.context, self.session, url)
self._hb_channel = self.hb_channel_class(
self.context, self.session, url
) # type:ignore[operator]
return self._hb_channel

@property
Expand All @@ -388,7 +400,9 @@ def control_channel(self) -> t.Any:
url = self._make_url("control")
self.log.debug("connecting control channel to %s", url)
socket = self.connect_control(identity=self.session.bsession)
self._control_channel = self.control_channel_class(socket, self.session, self.ioloop)
self._control_channel = self.control_channel_class(
socket, self.session, self.ioloop
) # type:ignore[operator]
return self._control_channel

async def _async_is_alive(self) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions jupyter_client/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def _data_dir_default(self):
to the Kernel, so be careful!""",
)

def _ip_default(self):
def _ip_default(self) -> str:
if self.transport == "ipc":
if self.connection_file:
return os.path.splitext(self.connection_file)[0] + "-ipc"
Expand Down Expand Up @@ -426,7 +426,7 @@ def get_connection_info(self, session: bool = False) -> KernelConnectionInfo:
def blocking_client(self):
"""Make a blocking client connected to my kernel"""
info = self.get_connection_info()
bc = self.blocking_class(parent=self)
bc = self.blocking_class(parent=self) # type:ignore[operator]
bc.load_connection_info(info)
return bc

Expand Down Expand Up @@ -540,7 +540,7 @@ def load_connection_info(self, info: KernelConnectionInfo) -> None:
See the connection_file spec for details.
"""
self.transport = info.get("transport", self.transport)
self.ip = info.get("ip", self._ip_default())
self.ip = info.get("ip", self._ip_default()) # type:ignore[assignment]

self._record_random_port_names()
for name in port_names:
Expand Down
5 changes: 3 additions & 2 deletions jupyter_client/consoleapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from . import KernelManager, connect, find_connection_file, tunnel_to_kernel
from .blocking import BlockingKernelClient
from .connect import KernelConnectionInfo
from .kernelspec import NoSuchKernel
from .localinterfaces import localhost
from .restarter import KernelRestarter
Expand Down Expand Up @@ -234,7 +235,7 @@ def init_ssh(self) -> None:
ip = localhost()

# build connection dict for tunnels:
info = {
info: KernelConnectionInfo = {
"ip": ip,
"shell_port": self.shell_port,
"iopub_port": self.iopub_port,
Expand Down Expand Up @@ -293,7 +294,7 @@ def init_kernel_manager(self) -> None:

# Create a KernelManager and start a kernel.
try:
self.kernel_manager = self.kernel_manager_class(
self.kernel_manager = self.kernel_manager_class( # type:ignore[operator]
ip=self.ip,
session=self.session,
transport=self.transport,
Expand Down
4 changes: 2 additions & 2 deletions jupyter_client/ioloop/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def start_restarter(self):
"""Start the restarter."""
if self.autorestart and self.has_kernel:
if self._restarter is None:
self._restarter = self.restarter_class(
self._restarter = self.restarter_class( # type:ignore[operator]
kernel_manager=self, loop=self.loop, parent=self, log=self.log
)
self._restarter.start()
Expand Down Expand Up @@ -99,7 +99,7 @@ def start_restarter(self):
"""Start the restarter."""
if self.autorestart and self.has_kernel:
if self._restarter is None:
self._restarter = self.restarter_class(
self._restarter = self.restarter_class( # type:ignore[operator]
kernel_manager=self, loop=self.loop, parent=self, log=self.log
)
self._restarter.start()
Expand Down
8 changes: 6 additions & 2 deletions jupyter_client/kernelspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,13 @@ def _get_kernel_spec_by_name(self, kernel_name, resource_dir):
pass
else:
if resource_dir == RESOURCES:
kspec = self.kernel_spec_class(resource_dir=resource_dir, **get_kernel_dict())
kspec = self.kernel_spec_class(
resource_dir=resource_dir, **get_kernel_dict()
) # type:ignore[operator]
if not kspec:
kspec = self.kernel_spec_class.from_resource_dir(resource_dir)
kspec = self.kernel_spec_class.from_resource_dir( # type:ignore[attr-defined]
resource_dir
)

if not KPF.instance(parent=self.parent).is_provisioner_available(kspec):
raise NoSuchKernel(kernel_name)
Expand Down
4 changes: 2 additions & 2 deletions jupyter_client/kernelspecapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _kernel_name_default(self):
"name": "InstallKernelSpec.kernel_name",
"prefix": "InstallKernelSpec.prefix",
}
aliases.update(base_aliases)
aliases.update(base_aliases) # type:ignore[arg-type]

flags = {
"user": (
Expand Down Expand Up @@ -185,7 +185,7 @@ def _kernel_spec_manager_default(self):
flags = {
"f": ({"RemoveKernelSpec": {"force": True}}, force.help),
}
flags.update(JupyterApp.flags)
flags.update(JupyterApp.flags) # type:ignore[has-type]

def parse_command_line(self, argv):
"""Parse the command line args."""
Expand Down
16 changes: 8 additions & 8 deletions jupyter_client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(self, *args, **kwargs):
# The PyZMQ Context to use for communication with the kernel.
context: Instance = Instance(zmq.Context)

@default("context") # type:ignore[misc]
@default("context")
def _context_default(self) -> zmq.Context:
self._created_context = True
return zmq.Context()
Expand All @@ -128,11 +128,11 @@ def _context_default(self) -> zmq.Context:
)
client_factory: Type = Type(klass=KernelClient)

@default("client_factory") # type:ignore[misc]
@default("client_factory")
def _client_factory_default(self) -> Type:
return import_item(self.client_class)

@observe("client_class") # type:ignore[misc]
@observe("client_class")
def _client_class_changed(self, change: t.Dict[str, DottedObjectName]) -> None:
self.client_factory = import_item(str(change["new"]))

Expand All @@ -145,11 +145,11 @@ def _client_class_changed(self, change: t.Dict[str, DottedObjectName]) -> None:

kernel_spec_manager: Instance = Instance(kernelspec.KernelSpecManager)

@default("kernel_spec_manager") # type:ignore[misc]
@default("kernel_spec_manager")
def _kernel_spec_manager_default(self) -> kernelspec.KernelSpecManager:
return kernelspec.KernelSpecManager(data_dir=self.data_dir)

@observe("kernel_spec_manager") # type:ignore[misc]
@observe("kernel_spec_manager")
@observe_compat # type:ignore[misc]
def _kernel_spec_manager_changed(self, change: t.Dict[str, Instance]) -> None:
self._kernel_spec = None
Expand All @@ -170,7 +170,7 @@ def _kernel_spec_manager_changed(self, change: t.Dict[str, Instance]) -> None:

kernel_name: t.Union[str, Unicode] = Unicode(kernelspec.NATIVE_KERNEL_NAME)

@observe("kernel_name") # type:ignore[misc]
@observe("kernel_name")
def _kernel_name_changed(self, change: t.Dict[str, str]) -> None:
self._kernel_spec = None
if change["new"] == "python":
Expand All @@ -190,7 +190,7 @@ def kernel_spec(self) -> t.Optional[kernelspec.KernelSpec]:
help="True if the MultiKernelManager should cache ports for this KernelManager instance",
)

@default("cache_ports") # type:ignore[misc]
@default("cache_ports")
def _default_cache_ports(self) -> bool:
return self.transport == "tcp"

Expand Down Expand Up @@ -688,7 +688,7 @@ class AsyncKernelManager(KernelManager):
# The PyZMQ Context to use for communication with the kernel.
context: Instance = Instance(zmq.asyncio.Context)

@default("context") # type:ignore[misc]
@default("context")
def _context_default(self) -> zmq.asyncio.Context:
self._created_context = True
return zmq.asyncio.Context()
Expand Down
4 changes: 2 additions & 2 deletions jupyter_client/multikernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _starting_kernels(self):
"""A shim for backwards compatibility."""
return self._pending_kernels

@default("context") # type:ignore[misc]
@default("context")
def _context_default(self) -> zmq.Context:
self._created_context = True
return zmq.Context()
Expand Down Expand Up @@ -602,7 +602,7 @@ class AsyncMultiKernelManager(MultiKernelManager):

context = Instance("zmq.asyncio.Context")

@default("context") # type:ignore[misc]
@default("context")
def _context_default(self) -> zmq.asyncio.Context:
self._created_context = True
return zmq.asyncio.Context()
Expand Down
2 changes: 1 addition & 1 deletion jupyter_client/runapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
frontend_flags = set(frontend_flags_dict.keys())


class RunApp(JupyterApp, JupyterConsoleApp):
class RunApp(JupyterApp, JupyterConsoleApp): # type:ignore[misc]
"""An Jupyter Console app to run files."""

version = __version__
Expand Down
Loading

0 comments on commit 6ea7118

Please sign in to comment.