3
3
# Distributed under the terms of the Modified BSD License.
4
4
import json
5
5
import re
6
- from typing import Any , Dict , List , Tuple
6
+ from typing import Any
7
7
8
8
from ._version import protocol_version_info
9
9
10
10
11
- def code_to_line (code : str , cursor_pos : int ) -> Tuple [str , int ]:
11
+ def code_to_line (code : str , cursor_pos : int ) -> tuple [str , int ]:
12
12
"""Turn a multiline code block and cursor position into a single line
13
13
and new cursor position.
14
14
@@ -59,32 +59,32 @@ class Adapter:
59
59
Override message_type(msg) methods to create adapters.
60
60
"""
61
61
62
- msg_type_map : Dict [str , str ] = {}
62
+ msg_type_map : dict [str , str ] = {}
63
63
64
- def update_header (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
64
+ def update_header (self , msg : dict [str , Any ]) -> dict [str , Any ]:
65
65
"""Update the header."""
66
66
return msg
67
67
68
- def update_metadata (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
68
+ def update_metadata (self , msg : dict [str , Any ]) -> dict [str , Any ]:
69
69
"""Update the metadata."""
70
70
return msg
71
71
72
- def update_msg_type (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
72
+ def update_msg_type (self , msg : dict [str , Any ]) -> dict [str , Any ]:
73
73
"""Update the message type."""
74
74
header = msg ["header" ]
75
75
msg_type = header ["msg_type" ]
76
76
if msg_type in self .msg_type_map :
77
77
msg ["msg_type" ] = header ["msg_type" ] = self .msg_type_map [msg_type ]
78
78
return msg
79
79
80
- def handle_reply_status_error (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
80
+ def handle_reply_status_error (self , msg : dict [str , Any ]) -> dict [str , Any ]:
81
81
"""This will be called *instead of* the regular handler
82
82
83
83
on any reply with status != ok
84
84
"""
85
85
return msg
86
86
87
- def __call__ (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
87
+ def __call__ (self , msg : dict [str , Any ]) -> dict [str , Any ]:
88
88
msg = self .update_header (msg )
89
89
msg = self .update_metadata (msg )
90
90
msg = self .update_msg_type (msg )
@@ -100,7 +100,7 @@ def __call__(self, msg: Dict[str, Any]) -> Dict[str, Any]:
100
100
return handler (msg )
101
101
102
102
103
- def _version_str_to_list (version : str ) -> List [int ]:
103
+ def _version_str_to_list (version : str ) -> list [int ]:
104
104
"""convert a version string to a list of ints
105
105
106
106
non-int segments are excluded
@@ -127,15 +127,15 @@ class V5toV4(Adapter):
127
127
"inspect_reply" : "object_info_reply" ,
128
128
}
129
129
130
- def update_header (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
130
+ def update_header (self , msg : dict [str , Any ]) -> dict [str , Any ]:
131
131
"""Update the header."""
132
132
msg ["header" ].pop ("version" , None )
133
133
msg ["parent_header" ].pop ("version" , None )
134
134
return msg
135
135
136
136
# shell channel
137
137
138
- def kernel_info_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
138
+ def kernel_info_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
139
139
"""Handle a kernel info reply."""
140
140
v4c = {}
141
141
content = msg ["content" ]
@@ -152,20 +152,20 @@ def kernel_info_reply(self, msg: Dict[str, Any]) -> Dict[str, Any]:
152
152
msg ["content" ] = v4c
153
153
return msg
154
154
155
- def execute_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
155
+ def execute_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
156
156
"""Handle an execute request."""
157
157
content = msg ["content" ]
158
158
content .setdefault ("user_variables" , [])
159
159
return msg
160
160
161
- def execute_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
161
+ def execute_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
162
162
"""Handle an execute reply."""
163
163
content = msg ["content" ]
164
164
content .setdefault ("user_variables" , {})
165
165
# TODO: handle payloads
166
166
return msg
167
167
168
- def complete_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
168
+ def complete_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
169
169
"""Handle a complete request."""
170
170
content = msg ["content" ]
171
171
code = content ["code" ]
@@ -179,7 +179,7 @@ def complete_request(self, msg: Dict[str, Any]) -> Dict[str, Any]:
179
179
new_content ["cursor_pos" ] = cursor_pos
180
180
return msg
181
181
182
- def complete_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
182
+ def complete_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
183
183
"""Handle a complete reply."""
184
184
content = msg ["content" ]
185
185
cursor_start = content .pop ("cursor_start" )
@@ -189,7 +189,7 @@ def complete_reply(self, msg: Dict[str, Any]) -> Dict[str, Any]:
189
189
content .pop ("metadata" , None )
190
190
return msg
191
191
192
- def object_info_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
192
+ def object_info_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
193
193
"""Handle an object info request."""
194
194
content = msg ["content" ]
195
195
code = content ["code" ]
@@ -201,20 +201,20 @@ def object_info_request(self, msg: Dict[str, Any]) -> Dict[str, Any]:
201
201
new_content ["detail_level" ] = content ["detail_level" ]
202
202
return msg
203
203
204
- def object_info_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
204
+ def object_info_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
205
205
"""inspect_reply can't be easily backward compatible"""
206
206
msg ["content" ] = {"found" : False , "oname" : "unknown" }
207
207
return msg
208
208
209
209
# iopub channel
210
210
211
- def stream (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
211
+ def stream (self , msg : dict [str , Any ]) -> dict [str , Any ]:
212
212
"""Handle a stream message."""
213
213
content = msg ["content" ]
214
214
content ["data" ] = content .pop ("text" )
215
215
return msg
216
216
217
- def display_data (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
217
+ def display_data (self , msg : dict [str , Any ]) -> dict [str , Any ]:
218
218
"""Handle a display data message."""
219
219
content = msg ["content" ]
220
220
content .setdefault ("source" , "display" )
@@ -229,7 +229,7 @@ def display_data(self, msg: Dict[str, Any]) -> Dict[str, Any]:
229
229
230
230
# stdin channel
231
231
232
- def input_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
232
+ def input_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
233
233
"""Handle an input request."""
234
234
msg ["content" ].pop ("password" , None )
235
235
return msg
@@ -243,7 +243,7 @@ class V4toV5(Adapter):
243
243
# invert message renames above
244
244
msg_type_map = {v : k for k , v in V5toV4 .msg_type_map .items ()}
245
245
246
- def update_header (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
246
+ def update_header (self , msg : dict [str , Any ]) -> dict [str , Any ]:
247
247
"""Update the header."""
248
248
msg ["header" ]["version" ] = self .version
249
249
if msg ["parent_header" ]:
@@ -252,7 +252,7 @@ def update_header(self, msg: Dict[str, Any]) -> Dict[str, Any]:
252
252
253
253
# shell channel
254
254
255
- def kernel_info_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
255
+ def kernel_info_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
256
256
"""Handle a kernel info reply."""
257
257
content = msg ["content" ]
258
258
for key in ("protocol_version" , "ipython_version" ):
@@ -275,7 +275,7 @@ def kernel_info_reply(self, msg: Dict[str, Any]) -> Dict[str, Any]:
275
275
content ["banner" ] = ""
276
276
return msg
277
277
278
- def execute_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
278
+ def execute_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
279
279
"""Handle an execute request."""
280
280
content = msg ["content" ]
281
281
user_variables = content .pop ("user_variables" , [])
@@ -284,7 +284,7 @@ def execute_request(self, msg: Dict[str, Any]) -> Dict[str, Any]:
284
284
user_expressions [v ] = v
285
285
return msg
286
286
287
- def execute_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
287
+ def execute_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
288
288
"""Handle an execute reply."""
289
289
content = msg ["content" ]
290
290
user_expressions = content .setdefault ("user_expressions" , {})
@@ -301,7 +301,7 @@ def execute_reply(self, msg: Dict[str, Any]) -> Dict[str, Any]:
301
301
302
302
return msg
303
303
304
- def complete_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
304
+ def complete_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
305
305
"""Handle a complete request."""
306
306
old_content = msg ["content" ]
307
307
@@ -310,7 +310,7 @@ def complete_request(self, msg: Dict[str, Any]) -> Dict[str, Any]:
310
310
new_content ["cursor_pos" ] = old_content ["cursor_pos" ]
311
311
return msg
312
312
313
- def complete_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
313
+ def complete_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
314
314
"""Handle a complete reply."""
315
315
# complete_reply needs more context than we have to get cursor_start and end.
316
316
# use special end=null to indicate current cursor position and negative offset
@@ -328,7 +328,7 @@ def complete_reply(self, msg: Dict[str, Any]) -> Dict[str, Any]:
328
328
new_content ["metadata" ] = {}
329
329
return msg
330
330
331
- def inspect_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
331
+ def inspect_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
332
332
"""Handle an inspect request."""
333
333
content = msg ["content" ]
334
334
name = content ["oname" ]
@@ -339,7 +339,7 @@ def inspect_request(self, msg: Dict[str, Any]) -> Dict[str, Any]:
339
339
new_content ["detail_level" ] = content ["detail_level" ]
340
340
return msg
341
341
342
- def inspect_reply (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
342
+ def inspect_reply (self , msg : dict [str , Any ]) -> dict [str , Any ]:
343
343
"""inspect_reply can't be easily backward compatible"""
344
344
content = msg ["content" ]
345
345
new_content = msg ["content" ] = {"status" : "ok" }
@@ -363,13 +363,13 @@ def inspect_reply(self, msg: Dict[str, Any]) -> Dict[str, Any]:
363
363
364
364
# iopub channel
365
365
366
- def stream (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
366
+ def stream (self , msg : dict [str , Any ]) -> dict [str , Any ]:
367
367
"""Handle a stream message."""
368
368
content = msg ["content" ]
369
369
content ["text" ] = content .pop ("data" )
370
370
return msg
371
371
372
- def display_data (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
372
+ def display_data (self , msg : dict [str , Any ]) -> dict [str , Any ]:
373
373
"""Handle display data."""
374
374
content = msg ["content" ]
375
375
content .pop ("source" , None )
@@ -384,13 +384,13 @@ def display_data(self, msg: Dict[str, Any]) -> Dict[str, Any]:
384
384
385
385
# stdin channel
386
386
387
- def input_request (self , msg : Dict [str , Any ]) -> Dict [str , Any ]:
387
+ def input_request (self , msg : dict [str , Any ]) -> dict [str , Any ]:
388
388
"""Handle an input request."""
389
389
msg ["content" ].setdefault ("password" , False )
390
390
return msg
391
391
392
392
393
- def adapt (msg : Dict [str , Any ], to_version : int = protocol_version_info [0 ]) -> Dict [str , Any ]:
393
+ def adapt (msg : dict [str , Any ], to_version : int = protocol_version_info [0 ]) -> dict [str , Any ]:
394
394
"""Adapt a single message to a target version
395
395
396
396
Parameters
0 commit comments