61
61
from ._version import kernel_protocol_version
62
62
from .iostream import OutStream
63
63
64
+ _AWAITABLE_MESSAGE : str = (
65
+ "For consistency across implementations, it is recommended that `{func_name}`"
66
+ " either be a coroutine function (`async def`) or return an awaitable object"
67
+ " (like an `asyncio.Future`). It might become a requirement in the future."
68
+ " Coroutine functions and awaitables have been supported since"
69
+ " ipykernel 6.0 (2021). {target} does not seem to return an awaitable"
70
+ )
71
+
64
72
65
73
def _accepts_parameters (meth , param_names ):
66
74
parameters = inspect .signature (meth ).parameters
@@ -842,6 +850,12 @@ async def execute_request(self, stream, ident, parent):
842
850
843
851
if inspect .isawaitable (reply_content ):
844
852
reply_content = await reply_content
853
+ else :
854
+ warnings .warn (
855
+ _AWAITABLE_MESSAGE .format (func_name = "do_execute" , target = self .do_execute ),
856
+ PendingDeprecationWarning ,
857
+ stacklevel = 1 ,
858
+ )
845
859
846
860
# Flush output before sending the reply.
847
861
if sys .stdout is not None :
@@ -898,6 +912,12 @@ async def complete_request(self, stream, ident, parent):
898
912
matches = self .do_complete (code , cursor_pos )
899
913
if inspect .isawaitable (matches ):
900
914
matches = await matches
915
+ else :
916
+ warnings .warn (
917
+ _AWAITABLE_MESSAGE .format (func_name = "do_complete" , target = self .do_complete ),
918
+ PendingDeprecationWarning ,
919
+ stacklevel = 1 ,
920
+ )
901
921
902
922
matches = json_clean (matches )
903
923
self .session .send (stream , "complete_reply" , matches , parent , ident )
@@ -926,6 +946,12 @@ async def inspect_request(self, stream, ident, parent):
926
946
)
927
947
if inspect .isawaitable (reply_content ):
928
948
reply_content = await reply_content
949
+ else :
950
+ warnings .warn (
951
+ _AWAITABLE_MESSAGE .format (func_name = "do_inspect" , target = self .do_inspect ),
952
+ PendingDeprecationWarning ,
953
+ stacklevel = 1 ,
954
+ )
929
955
930
956
# Before we send this object over, we scrub it for JSON usage
931
957
reply_content = json_clean (reply_content )
@@ -945,6 +971,12 @@ async def history_request(self, stream, ident, parent):
945
971
reply_content = self .do_history (** content )
946
972
if inspect .isawaitable (reply_content ):
947
973
reply_content = await reply_content
974
+ else :
975
+ warnings .warn (
976
+ _AWAITABLE_MESSAGE .format (func_name = "do_history" , target = self .do_history ),
977
+ PendingDeprecationWarning ,
978
+ stacklevel = 1 ,
979
+ )
948
980
949
981
reply_content = json_clean (reply_content )
950
982
msg = self .session .send (stream , "history_reply" , reply_content , parent , ident )
@@ -1067,7 +1099,13 @@ async def shutdown_request(self, stream, ident, parent):
1067
1099
content = self .do_shutdown (parent ["content" ]["restart" ])
1068
1100
if inspect .isawaitable (content ):
1069
1101
content = await content
1070
- self .session .send (stream , "shutdown_reply" , content , parent , ident = ident )
1102
+ else :
1103
+ warnings .warn (
1104
+ _AWAITABLE_MESSAGE .format (func_name = "do_shutdown" , target = self .do_shutdown ),
1105
+ PendingDeprecationWarning ,
1106
+ stacklevel = 1 ,
1107
+ )
1108
+ self .session .send (socket , "shutdown_reply" , content , parent , ident = ident )
1071
1109
# same content, but different msg_id for broadcasting on IOPub
1072
1110
self ._shutdown_message = self .session .msg ("shutdown_reply" , content , parent )
1073
1111
@@ -1100,6 +1138,12 @@ async def is_complete_request(self, stream, ident, parent):
1100
1138
reply_content = self .do_is_complete (code )
1101
1139
if inspect .isawaitable (reply_content ):
1102
1140
reply_content = await reply_content
1141
+ else :
1142
+ warnings .warn (
1143
+ _AWAITABLE_MESSAGE .format (func_name = "do_is_complete" , target = self .do_is_complete ),
1144
+ PendingDeprecationWarning ,
1145
+ stacklevel = 1 ,
1146
+ )
1103
1147
reply_content = json_clean (reply_content )
1104
1148
reply_msg = self .session .send (stream , "is_complete_reply" , reply_content , parent , ident )
1105
1149
self .log .debug ("%s" , reply_msg )
@@ -1116,6 +1160,14 @@ async def debug_request(self, stream, ident, parent):
1116
1160
reply_content = self .do_debug_request (content )
1117
1161
if inspect .isawaitable (reply_content ):
1118
1162
reply_content = await reply_content
1163
+ else :
1164
+ warnings .warn (
1165
+ _AWAITABLE_MESSAGE .format (
1166
+ func_name = "do_debug_request" , target = self .do_debug_request
1167
+ ),
1168
+ PendingDeprecationWarning ,
1169
+ stacklevel = 1 ,
1170
+ )
1119
1171
reply_content = json_clean (reply_content )
1120
1172
reply_msg = self .session .send (stream , "debug_reply" , reply_content , parent , ident )
1121
1173
self .log .debug ("%s" , reply_msg )
0 commit comments