Skip to content

Commit

Permalink
Enable strict typing (#984)
Browse files Browse the repository at this point in the history
* Use strict typing

* fix imports

* ignore pep585 and 604 typing changes
  • Loading branch information
blink1073 authored Oct 25, 2023
1 parent 49dbf0f commit ff94e31
Show file tree
Hide file tree
Showing 31 changed files with 365 additions and 271 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def filter(self, record: pylogging.LogRecord) -> bool:
intersphinx_mapping = {'ipython': ('http://ipython.readthedocs.io/en/stable/', None)}


def setup(app):
def setup(app: object) -> None:
HERE = osp.abspath(osp.dirname(__file__))
dest = osp.join(HERE, 'changelog.md')
shutil.copy(osp.join(HERE, '..', 'CHANGELOG.md'), dest)
7 changes: 5 additions & 2 deletions jupyter_client/asynchronous/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Implements an async kernel client"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import typing as t

import zmq.asyncio
from traitlets import Instance, Type
Expand All @@ -9,10 +12,10 @@
from ..client import KernelClient, reqrep


def wrapped(meth, channel):
def wrapped(meth: t.Callable, channel: str) -> t.Callable:
"""Wrap a method on a channel and handle replies."""

def _(self, *args, **kwargs):
def _(self: AsyncKernelClient, *args: t.Any, **kwargs: t.Any) -> t.Any:
reply = kwargs.pop("reply", False)
timeout = kwargs.pop("timeout", None)
msg_id = meth(self, *args, **kwargs)
Expand Down
8 changes: 6 additions & 2 deletions jupyter_client/blocking/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import typing as t

from traitlets import Type

from ..channels import HBChannel, ZMQSocketChannel
from ..client import KernelClient, reqrep
from ..utils import run_sync


def wrapped(meth, channel):
def wrapped(meth: t.Callable, channel: str) -> t.Callable:
"""Wrap a method on a channel and handle replies."""

def _(self, *args, **kwargs):
def _(self: BlockingKernelClient, *args: t.Any, **kwargs: t.Any) -> t.Any:
reply = kwargs.pop("reply", False)
timeout = kwargs.pop("timeout", None)
msg_id = meth(self, *args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion jupyter_client/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
context: t.Optional[zmq.Context] = None,
session: t.Optional[Session] = None,
address: t.Union[t.Tuple[str, int], str] = "",
):
) -> None:
"""Create the heartbeat monitor thread.
Parameters
Expand Down
14 changes: 7 additions & 7 deletions jupyter_client/channelsabc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class ChannelABC(metaclass=abc.ABCMeta):
"""A base class for all channel ABCs."""

@abc.abstractmethod
def start(self):
def start(self) -> None:
"""Start the channel."""
pass

@abc.abstractmethod
def stop(self):
def stop(self) -> None:
"""Stop the channel."""
pass

@abc.abstractmethod
def is_alive(self):
def is_alive(self) -> bool:
"""Test whether the channel is alive."""
pass

Expand All @@ -32,20 +32,20 @@ class HBChannelABC(ChannelABC):
"""

@abc.abstractproperty
def time_to_dead(self):
def time_to_dead(self) -> float:
pass

@abc.abstractmethod
def pause(self):
def pause(self) -> None:
"""Pause the heartbeat channel."""
pass

@abc.abstractmethod
def unpause(self):
def unpause(self) -> None:
"""Unpause the heartbeat channel."""
pass

@abc.abstractmethod
def is_beating(self):
def is_beating(self) -> bool:
"""Test whether the channel is beating."""
pass
4 changes: 2 additions & 2 deletions jupyter_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _context_default(self) -> zmq.Context:
# flag for whether execute requests should be allowed to call raw_input:
allow_stdin: bool = True

def __del__(self):
def __del__(self) -> None:
"""Handle garbage collection. Destroy context if applicable."""
if (
self._created_context
Expand Down Expand Up @@ -511,7 +511,7 @@ async def _async_execute_interactive(
if output_hook is None and "IPython" in sys.modules:
from IPython import get_ipython

ip = get_ipython()
ip = get_ipython() # type:ignore[no-untyped-call]
in_kernel = getattr(ip, "kernel", False)
if in_kernel:
output_hook = partial(
Expand Down
41 changes: 27 additions & 14 deletions jupyter_client/clientabc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
from __future__ import annotations

import abc
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from .channelsabc import ChannelABC

# -----------------------------------------------------------------------------
# Main kernel client class
Expand All @@ -24,64 +30,71 @@ class KernelClientABC(metaclass=abc.ABCMeta):
"""

@abc.abstractproperty
def kernel(self):
def kernel(self) -> Any:
pass

@abc.abstractproperty
def shell_channel_class(self):
def shell_channel_class(self) -> type[ChannelABC]:
pass

@abc.abstractproperty
def iopub_channel_class(self):
def iopub_channel_class(self) -> type[ChannelABC]:
pass

@abc.abstractproperty
def hb_channel_class(self):
def hb_channel_class(self) -> type[ChannelABC]:
pass

@abc.abstractproperty
def stdin_channel_class(self):
def stdin_channel_class(self) -> type[ChannelABC]:
pass

@abc.abstractproperty
def control_channel_class(self):
def control_channel_class(self) -> type[ChannelABC]:
pass

# --------------------------------------------------------------------------
# Channel management methods
# --------------------------------------------------------------------------

@abc.abstractmethod
def start_channels(self, shell=True, iopub=True, stdin=True, hb=True, control=True):
def start_channels(
self,
shell: bool = True,
iopub: bool = True,
stdin: bool = True,
hb: bool = True,
control: bool = True,
) -> None:
"""Start the channels for the client."""
pass

@abc.abstractmethod
def stop_channels(self):
def stop_channels(self) -> None:
"""Stop the channels for the client."""
pass

@abc.abstractproperty
def channels_running(self):
def channels_running(self) -> bool:
"""Get whether the channels are running."""
pass

@abc.abstractproperty
def shell_channel(self):
def shell_channel(self) -> ChannelABC:
pass

@abc.abstractproperty
def iopub_channel(self):
def iopub_channel(self) -> ChannelABC:
pass

@abc.abstractproperty
def stdin_channel(self):
def stdin_channel(self) -> ChannelABC:
pass

@abc.abstractproperty
def hb_channel(self):
def hb_channel(self) -> ChannelABC:
pass

@abc.abstractproperty
def control_channel(self):
def control_channel(self) -> ChannelABC:
pass
Loading

0 comments on commit ff94e31

Please sign in to comment.