Skip to content

Commit 4569594

Browse files
committed
More file checking
1 parent 2f6a820 commit 4569594

File tree

6 files changed

+76
-27
lines changed

6 files changed

+76
-27
lines changed

nbclient/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ._version import version as __version__ # noqa: F401
55

66

7-
def _cleanup():
7+
def _cleanup() -> None:
88
pass
99

1010

nbclient/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ def _get_buffer_data(self, msg: t.Dict) -> t.List[t.Dict[str, str]]:
992992
def register_output_hook(
993993
self,
994994
msg_id: str,
995-
hook: t.Callable) -> None:
995+
hook: OutputWidget) -> None:
996996
"""Registers an override object that handles output/clear_output instead.
997997
998998
Multiple hooks can be registered, where the last one will be used (stack based)
@@ -1004,7 +1004,7 @@ def register_output_hook(
10041004
def remove_output_hook(
10051005
self,
10061006
msg_id: str,
1007-
hook: t.Callable) -> None:
1007+
hook: OutputWidget) -> None:
10081008
"""Unregisters an override object that handles output/clear_output instead"""
10091009
# mimics
10101010
# https://jupyterlab.github.io/jupyterlab/services/interfaces/kernel.ikernelconnection.html#removemessagehook

nbclient/exceptions.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from typing import Dict
2+
3+
from nbformat import NotebookNode
4+
5+
16
class CellControlSignal(Exception):
27
"""
38
A custom exception used to indicate that the exception is used for cell
@@ -13,7 +18,11 @@ class CellTimeoutError(TimeoutError, CellControlSignal):
1318
"""
1419

1520
@classmethod
16-
def error_from_timeout_and_cell(cls, msg, timeout, cell):
21+
def error_from_timeout_and_cell(
22+
cls,
23+
msg: str,
24+
timeout: int,
25+
cell: NotebookNode) -> CellTimeoutError:
1726
if cell and cell.source:
1827
src_by_lines = cell.source.strip().split("\n")
1928
src = (
@@ -49,23 +58,30 @@ class CellExecutionError(CellControlSignal):
4958
failures gracefully.
5059
"""
5160

52-
def __init__(self, traceback, ename, evalue):
61+
def __init__(
62+
self,
63+
traceback: str,
64+
ename: str,
65+
evalue: str) -> None:
5366
super(CellExecutionError, self).__init__(traceback)
5467
self.traceback = traceback
5568
self.ename = ename
5669
self.evalue = evalue
5770

58-
def __str__(self):
71+
def __str__(self) -> str:
5972
s = self.__unicode__()
6073
if not isinstance(s, str):
6174
s = s.encode('utf8', 'replace')
6275
return s
6376

64-
def __unicode__(self):
77+
def __unicode__(self) -> str:
6578
return self.traceback
6679

6780
@classmethod
68-
def from_cell_and_msg(cls, cell, msg):
81+
def from_cell_and_msg(
82+
cls,
83+
cell: NotebookNode,
84+
msg: Dict) -> CellExecutionError:
6985
"""Instantiate from a code cell object and a message contents
7086
(message is either execute_reply or error)
7187
"""
@@ -82,7 +98,7 @@ def from_cell_and_msg(cls, cell, msg):
8298
)
8399

84100

85-
exec_err_msg = u"""\
101+
exec_err_msg: str = u"""\
86102
An error occurred while executing the following cell:
87103
------------------
88104
{cell.source}
@@ -93,7 +109,7 @@ def from_cell_and_msg(cls, cell, msg):
93109
"""
94110

95111

96-
timeout_err_msg = u"""\
112+
timeout_err_msg: str = u"""\
97113
A cell timed out while it was being executed, after {timeout} seconds.
98114
The message was: {msg}.
99115
Here is a preview of the cell contents:

nbclient/jsonutil.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import types
1212
from datetime import datetime
1313
import numbers
14+
from typing import Dict
1415

1516

1617
from ipython_genutils import py3compat
@@ -51,7 +52,7 @@
5152
PDF64 = b'JVBER'
5253

5354

54-
def encode_images(format_dict):
55+
def encode_images(format_dict: Dict) -> Dict[str, str]:
5556
"""b64-encodes images in a displaypub format dict
5657
5758
Perhaps this should be handled in json_clean itself?

nbclient/output_widget.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
from .jsonutil import json_clean
22
from nbformat.v4 import output_from_msg
3+
from typing import Dict, List, Any, Optional
4+
5+
from jupyter_client.client import KernelClient
6+
from nbclient import NotebookClient
37

48

59
class OutputWidget:
610
"""This class mimics a front end output widget"""
7-
def __init__(self, comm_id, state, kernel_client, executor):
8-
self.comm_id = comm_id
9-
self.state = state
10-
self.kernel_client = kernel_client
11-
self.executor = executor
12-
self.topic = ('comm-%s' % self.comm_id).encode('ascii')
13-
self.outputs = self.state['outputs']
14-
self.clear_before_next_output = False
11+
def __init__(
12+
self,
13+
comm_id: str,
14+
state: Dict[str, Any],
15+
kernel_client: KernelClient,
16+
executor: NotebookClient) -> None:
17+
18+
self.comm_id: str = comm_id
19+
self.state: Dict[str, Any] = state
20+
self.kernel_client: KernelClient = kernel_client
21+
self.executor: NotebookClient = executor
22+
self.topic: bytes = ('comm-%s' % self.comm_id).encode('ascii')
23+
self.outputs: List = self.state['outputs']
24+
self.clear_before_next_output: bool = False
25+
26+
def clear_output(
27+
self,
28+
outs: List,
29+
msg: Dict,
30+
cell_index: int) -> None:
1531

16-
def clear_output(self, outs, msg, cell_index):
1732
self.parent_header = msg['parent_header']
1833
content = msg['content']
1934
if content.get('wait'):
@@ -26,12 +41,18 @@ def clear_output(self, outs, msg, cell_index):
2641
# sync the state to the nbconvert state as well, since that is used for testing
2742
self.executor.widget_state[self.comm_id]['outputs'] = self.outputs
2843

29-
def sync_state(self):
44+
def sync_state(self) -> None:
3045
state = {'outputs': self.outputs}
3146
msg = {'method': 'update', 'state': state, 'buffer_paths': []}
3247
self.send(msg)
3348

34-
def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
49+
def _publish_msg(
50+
self,
51+
msg_type: str,
52+
data: Optional[Dict] = None,
53+
metadata: Optional[Dict] = None,
54+
buffers: Optional[List] = None,
55+
**keys) -> None:
3556
"""Helper for sending a comm message on IOPub"""
3657
data = {} if data is None else data
3758
metadata = {} if metadata is None else metadata
@@ -40,10 +61,21 @@ def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys)
4061
metadata=metadata)
4162
self.kernel_client.shell_channel.send(msg)
4263

43-
def send(self, data=None, metadata=None, buffers=None):
64+
def send(
65+
self,
66+
data: Optional[Dict] = None,
67+
metadata: Optional[Dict] = None,
68+
buffers: Optional[List] = None) -> None:
69+
4470
self._publish_msg('comm_msg', data=data, metadata=metadata, buffers=buffers)
4571

46-
def output(self, outs, msg, display_id, cell_index):
72+
def output(
73+
self,
74+
outs: List,
75+
msg: Dict,
76+
display_id: str,
77+
cell_index: int) -> None:
78+
4779
if self.clear_before_next_output:
4880
self.outputs = []
4981
self.clear_before_next_output = False
@@ -66,7 +98,7 @@ def output(self, outs, msg, display_id, cell_index):
6698
# sync the state to the nbconvert state as well, since that is used for testing
6799
self.executor.widget_state[self.comm_id]['outputs'] = self.outputs
68100

69-
def set_state(self, state):
101+
def set_state(self, state: Dict) -> None:
70102
if 'msg_id' in state:
71103
msg_id = state.get('msg_id')
72104
if msg_id:
@@ -76,7 +108,7 @@ def set_state(self, state):
76108
self.executor.remove_output_hook(self.msg_id, self)
77109
self.msg_id = msg_id
78110

79-
def handle_msg(self, msg):
111+
def handle_msg(self, msg: Dict) -> None:
80112
content = msg['content']
81113
comm_id = content['comm_id']
82114
assert comm_id == self.comm_id

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ commands = flake8 nbclient --count --ignore=E203,E731,F811,W503 --max-complexity
1818
[testenv:mypy]
1919
skip_install = true
2020
deps = mypy
21-
commands = mypy nbclient/client.py nbclient/util.py
21+
commands = mypy nbclient/*.py
2222

2323
# Manifest
2424
[testenv:manifest]

0 commit comments

Comments
 (0)