Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable strict typing #984

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading