5
5
6
6
import re
7
7
import json
8
+ from typing import List , Tuple , Dict , Any
8
9
9
10
from jupyter_client import protocol_version_info
10
11
11
- def code_to_line (code , cursor_pos ):
12
+ def code_to_line (
13
+ code : str ,
14
+ cursor_pos : int
15
+ ) -> Tuple [str , int ]:
12
16
"""Turn a multiline code block and cursor position into a single line
13
17
and new cursor position.
14
18
@@ -29,14 +33,17 @@ def code_to_line(code, cursor_pos):
29
33
_end_bracket = re .compile (r'\([^\(]*$' , re .UNICODE )
30
34
_identifier = re .compile (r'[a-z_][0-9a-z._]*' , re .I | re .UNICODE )
31
35
32
- def extract_oname_v4 (code , cursor_pos ):
36
+ def extract_oname_v4 (
37
+ code : str ,
38
+ cursor_pos : int
39
+ ) -> str :
33
40
"""Reimplement token-finding logic from IPython 2.x javascript
34
-
41
+
35
42
for adapting object_info_request from v5 to v4
36
43
"""
37
-
44
+
38
45
line , _ = code_to_line (code , cursor_pos )
39
-
46
+
40
47
oldline = line
41
48
line = _match_bracket .sub ('' , line )
42
49
while oldline != line :
@@ -58,29 +65,44 @@ class Adapter(object):
58
65
Override message_type(msg) methods to create adapters.
59
66
"""
60
67
61
- msg_type_map = {}
68
+ msg_type_map : Dict [ str , str ] = {}
62
69
63
- def update_header (self , msg ):
70
+ def update_header (
71
+ self ,
72
+ msg : Dict [str , Any ]
73
+ ) -> Dict [str , Any ]:
64
74
return msg
65
75
66
- def update_metadata (self , msg ):
76
+ def update_metadata (
77
+ self ,
78
+ msg : Dict [str , Any ]
79
+ ) -> Dict [str , Any ]:
67
80
return msg
68
81
69
- def update_msg_type (self , msg ):
82
+ def update_msg_type (
83
+ self ,
84
+ msg : Dict [str , Any ]
85
+ ) -> Dict [str , Any ]:
70
86
header = msg ['header' ]
71
87
msg_type = header ['msg_type' ]
72
88
if msg_type in self .msg_type_map :
73
89
msg ['msg_type' ] = header ['msg_type' ] = self .msg_type_map [msg_type ]
74
90
return msg
75
91
76
- def handle_reply_status_error (self , msg ):
92
+ def handle_reply_status_error (
93
+ self ,
94
+ msg : Dict [str , Any ]
95
+ ) -> Dict [str , Any ]:
77
96
"""This will be called *instead of* the regular handler
78
97
79
98
on any reply with status != ok
80
99
"""
81
100
return msg
82
101
83
- def __call__ (self , msg ):
102
+ def __call__ (
103
+ self ,
104
+ msg : Dict [str , Any ]
105
+ ):
84
106
msg = self .update_header (msg )
85
107
msg = self .update_metadata (msg )
86
108
msg = self .update_msg_type (msg )
@@ -95,7 +117,9 @@ def __call__(self, msg):
95
117
return self .handle_reply_status_error (msg )
96
118
return handler (msg )
97
119
98
- def _version_str_to_list (version ):
120
+ def _version_str_to_list (
121
+ version : str
122
+ ) -> List [int ]:
99
123
"""convert a version string to a list of ints
100
124
101
125
non-int segments are excluded
@@ -121,14 +145,20 @@ class V5toV4(Adapter):
121
145
'inspect_reply' : 'object_info_reply' ,
122
146
}
123
147
124
- def update_header (self , msg ):
148
+ def update_header (
149
+ self ,
150
+ msg : Dict [str , Any ]
151
+ ) -> Dict [str , Any ]:
125
152
msg ['header' ].pop ('version' , None )
126
153
msg ['parent_header' ].pop ('version' , None )
127
154
return msg
128
155
129
156
# shell channel
130
157
131
- def kernel_info_reply (self , msg ):
158
+ def kernel_info_reply (
159
+ self ,
160
+ msg : Dict [str , Any ]
161
+ ) -> Dict [str , Any ]:
132
162
v4c = {}
133
163
content = msg ['content' ]
134
164
for key in ('language_version' , 'protocol_version' ):
@@ -145,18 +175,27 @@ def kernel_info_reply(self, msg):
145
175
msg ['content' ] = v4c
146
176
return msg
147
177
148
- def execute_request (self , msg ):
178
+ def execute_request (
179
+ self ,
180
+ msg : Dict [str , Any ]
181
+ ) -> Dict [str , Any ]:
149
182
content = msg ['content' ]
150
183
content .setdefault ('user_variables' , [])
151
184
return msg
152
185
153
- def execute_reply (self , msg ):
186
+ def execute_reply (
187
+ self ,
188
+ msg : Dict [str , Any ]
189
+ ) -> Dict [str , Any ]:
154
190
content = msg ['content' ]
155
191
content .setdefault ('user_variables' , {})
156
192
# TODO: handle payloads
157
193
return msg
158
194
159
- def complete_request (self , msg ):
195
+ def complete_request (
196
+ self ,
197
+ msg : Dict [str , Any ]
198
+ ) -> Dict [str , Any ]:
160
199
content = msg ['content' ]
161
200
code = content ['code' ]
162
201
cursor_pos = content ['cursor_pos' ]
@@ -169,7 +208,10 @@ def complete_request(self, msg):
169
208
new_content ['cursor_pos' ] = cursor_pos
170
209
return msg
171
210
172
- def complete_reply (self , msg ):
211
+ def complete_reply (
212
+ self ,
213
+ msg : Dict [str , Any ]
214
+ ) -> Dict [str , Any ]:
173
215
content = msg ['content' ]
174
216
cursor_start = content .pop ('cursor_start' )
175
217
cursor_end = content .pop ('cursor_end' )
@@ -178,7 +220,10 @@ def complete_reply(self, msg):
178
220
content .pop ('metadata' , None )
179
221
return msg
180
222
181
- def object_info_request (self , msg ):
223
+ def object_info_request (
224
+ self ,
225
+ msg : Dict [str , Any ]
226
+ ) -> Dict [str , Any ]:
182
227
content = msg ['content' ]
183
228
code = content ['code' ]
184
229
cursor_pos = content ['cursor_pos' ]
@@ -189,19 +234,28 @@ def object_info_request(self, msg):
189
234
new_content ['detail_level' ] = content ['detail_level' ]
190
235
return msg
191
236
192
- def object_info_reply (self , msg ):
237
+ def object_info_reply (
238
+ self ,
239
+ msg : Dict [str , Any ]
240
+ ) -> Dict [str , Any ]:
193
241
"""inspect_reply can't be easily backward compatible"""
194
242
msg ['content' ] = {'found' : False , 'oname' : 'unknown' }
195
243
return msg
196
244
197
245
# iopub channel
198
246
199
- def stream (self , msg ):
247
+ def stream (
248
+ self ,
249
+ msg : Dict [str , Any ]
250
+ ) -> Dict [str , Any ]:
200
251
content = msg ['content' ]
201
252
content ['data' ] = content .pop ('text' )
202
253
return msg
203
254
204
- def display_data (self , msg ):
255
+ def display_data (
256
+ self ,
257
+ msg : Dict [str , Any ]
258
+ ) -> Dict [str , Any ]:
205
259
content = msg ['content' ]
206
260
content .setdefault ("source" , "display" )
207
261
data = content ['data' ]
@@ -215,7 +269,10 @@ def display_data(self, msg):
215
269
216
270
# stdin channel
217
271
218
- def input_request (self , msg ):
272
+ def input_request (
273
+ self ,
274
+ msg : Dict [str , Any ]
275
+ ) -> Dict [str , Any ]:
219
276
msg ['content' ].pop ('password' , None )
220
277
return msg
221
278
@@ -227,15 +284,21 @@ class V4toV5(Adapter):
227
284
# invert message renames above
228
285
msg_type_map = {v :k for k ,v in V5toV4 .msg_type_map .items ()}
229
286
230
- def update_header (self , msg ):
287
+ def update_header (
288
+ self ,
289
+ msg : Dict [str , Any ]
290
+ ) -> Dict [str , Any ]:
231
291
msg ['header' ]['version' ] = self .version
232
292
if msg ['parent_header' ]:
233
293
msg ['parent_header' ]['version' ] = self .version
234
294
return msg
235
295
236
296
# shell channel
237
297
238
- def kernel_info_reply (self , msg ):
298
+ def kernel_info_reply (
299
+ self ,
300
+ msg : Dict [str , Any ]
301
+ ) -> Dict [str , Any ]:
239
302
content = msg ['content' ]
240
303
for key in ('protocol_version' , 'ipython_version' ):
241
304
if key in content :
@@ -257,15 +320,21 @@ def kernel_info_reply(self, msg):
257
320
content ['banner' ] = ''
258
321
return msg
259
322
260
- def execute_request (self , msg ):
323
+ def execute_request (
324
+ self ,
325
+ msg : Dict [str , Any ]
326
+ ) -> Dict [str , Any ]:
261
327
content = msg ['content' ]
262
328
user_variables = content .pop ('user_variables' , [])
263
329
user_expressions = content .setdefault ('user_expressions' , {})
264
330
for v in user_variables :
265
331
user_expressions [v ] = v
266
332
return msg
267
333
268
- def execute_reply (self , msg ):
334
+ def execute_reply (
335
+ self ,
336
+ msg : Dict [str , Any ]
337
+ ) -> Dict [str , Any ]:
269
338
content = msg ['content' ]
270
339
user_expressions = content .setdefault ('user_expressions' , {})
271
340
user_variables = content .pop ('user_variables' , {})
@@ -281,15 +350,21 @@ def execute_reply(self, msg):
281
350
282
351
return msg
283
352
284
- def complete_request (self , msg ):
353
+ def complete_request (
354
+ self ,
355
+ msg : Dict [str , Any ]
356
+ ) -> Dict [str , Any ]:
285
357
old_content = msg ['content' ]
286
358
287
359
new_content = msg ['content' ] = {}
288
360
new_content ['code' ] = old_content ['line' ]
289
361
new_content ['cursor_pos' ] = old_content ['cursor_pos' ]
290
362
return msg
291
363
292
- def complete_reply (self , msg ):
364
+ def complete_reply (
365
+ self ,
366
+ msg : Dict [str , Any ]
367
+ ) -> Dict [str , Any ]:
293
368
# complete_reply needs more context than we have to get cursor_start and end.
294
369
# use special end=null to indicate current cursor position and negative offset
295
370
# for start relative to the cursor.
@@ -306,7 +381,10 @@ def complete_reply(self, msg):
306
381
new_content ['metadata' ] = {}
307
382
return msg
308
383
309
- def inspect_request (self , msg ):
384
+ def inspect_request (
385
+ self ,
386
+ msg : Dict [str , Any ]
387
+ ) -> Dict [str , Any ]:
310
388
content = msg ['content' ]
311
389
name = content ['oname' ]
312
390
@@ -316,7 +394,10 @@ def inspect_request(self, msg):
316
394
new_content ['detail_level' ] = content ['detail_level' ]
317
395
return msg
318
396
319
- def inspect_reply (self , msg ):
397
+ def inspect_reply (
398
+ self ,
399
+ msg : Dict [str , Any ]
400
+ ) -> Dict [str , Any ]:
320
401
"""inspect_reply can't be easily backward compatible"""
321
402
content = msg ['content' ]
322
403
new_content = msg ['content' ] = {'status' : 'ok' }
@@ -340,12 +421,18 @@ def inspect_reply(self, msg):
340
421
341
422
# iopub channel
342
423
343
- def stream (self , msg ):
424
+ def stream (
425
+ self ,
426
+ msg : Dict [str , Any ]
427
+ ) -> Dict [str , Any ]:
344
428
content = msg ['content' ]
345
429
content ['text' ] = content .pop ('data' )
346
430
return msg
347
431
348
- def display_data (self , msg ):
432
+ def display_data (
433
+ self ,
434
+ msg : Dict [str , Any ]
435
+ ) -> Dict [str , Any ]:
349
436
content = msg ['content' ]
350
437
content .pop ("source" , None )
351
438
data = content ['data' ]
@@ -359,13 +446,19 @@ def display_data(self, msg):
359
446
360
447
# stdin channel
361
448
362
- def input_request (self , msg ):
449
+ def input_request (
450
+ self ,
451
+ msg : Dict [str , Any ]
452
+ ) -> Dict [str , Any ]:
363
453
msg ['content' ].setdefault ('password' , False )
364
454
return msg
365
455
366
456
367
457
368
- def adapt (msg , to_version = protocol_version_info [0 ]):
458
+ def adapt (
459
+ msg : Dict [str , Any ],
460
+ to_version : int = protocol_version_info [0 ]
461
+ ) -> Dict [str , Any ]:
369
462
"""Adapt a single message to a target version
370
463
371
464
Parameters
0 commit comments