57
57
from ._version import kernel_protocol_version
58
58
from .iostream import OutStream
59
59
60
+ _AWAITABLE_MESSAGE = (
61
+ "For consistency across implementations, it is recommended that `{func_name}`"
62
+ " either be a coroutine function (async def) or return an awaitable object"
63
+ " (like a Future) function (`async def`) it might become a requirement in"
64
+ " the future."
65
+ " Coroutine functions and awaitable have been supported since"
66
+ " ipykernel 6.0 (2021)." ,
67
+ )
68
+
60
69
61
70
def _accepts_parameters (meth , param_names ):
62
71
parameters = inspect .signature (meth ).parameters
@@ -742,6 +751,12 @@ async def execute_request(self, socket, ident, parent):
742
751
743
752
if inspect .isawaitable (reply_content ):
744
753
reply_content = await reply_content
754
+ else :
755
+ warnings .warn (
756
+ _AWAITABLE_MESSAGE .format (func_name = "execute_request" ),
757
+ PendingDeprecationWarning ,
758
+ stacklevel = 1 ,
759
+ )
745
760
746
761
# Flush output before sending the reply.
747
762
if sys .stdout is not None :
@@ -802,11 +817,17 @@ async def complete_request(self, socket, ident, parent):
802
817
matches = self .do_complete (code , cursor_pos )
803
818
if inspect .isawaitable (matches ):
804
819
matches = await matches
820
+ else :
821
+ warnings .warn (
822
+ _AWAITABLE_MESSAGE .format (func_name = "do_complete" ),
823
+ PendingDeprecationWarning ,
824
+ stacklevel = 1 ,
825
+ )
805
826
806
827
matches = json_clean (matches )
807
828
self .session .send (socket , "complete_reply" , matches , parent , ident )
808
829
809
- def do_complete (self , code , cursor_pos ):
830
+ async def do_complete (self , code , cursor_pos ):
810
831
"""Override in subclasses to find completions."""
811
832
return {
812
833
"matches" : [],
@@ -830,6 +851,12 @@ async def inspect_request(self, socket, ident, parent):
830
851
)
831
852
if inspect .isawaitable (reply_content ):
832
853
reply_content = await reply_content
854
+ else :
855
+ warnings .warn (
856
+ _AWAITABLE_MESSAGE .format (func_name = "inspect_request" ),
857
+ PendingDeprecationWarning ,
858
+ stacklevel = 1 ,
859
+ )
833
860
834
861
# Before we send this object over, we scrub it for JSON usage
835
862
reply_content = json_clean (reply_content )
@@ -849,6 +876,12 @@ async def history_request(self, socket, ident, parent):
849
876
reply_content = self .do_history (** content )
850
877
if inspect .isawaitable (reply_content ):
851
878
reply_content = await reply_content
879
+ else :
880
+ warnings .warn (
881
+ _AWAITABLE_MESSAGE .format (func_name = "history_request" ),
882
+ PendingDeprecationWarning ,
883
+ stacklevel = 1 ,
884
+ )
852
885
853
886
reply_content = json_clean (reply_content )
854
887
msg = self .session .send (socket , "history_reply" , reply_content , parent , ident )
@@ -966,6 +999,12 @@ async def shutdown_request(self, socket, ident, parent):
966
999
content = self .do_shutdown (parent ["content" ]["restart" ])
967
1000
if inspect .isawaitable (content ):
968
1001
content = await content
1002
+ else :
1003
+ warnings .warn (
1004
+ _AWAITABLE_MESSAGE .format (func_name = "do_shutdown" ),
1005
+ PendingDeprecationWarning ,
1006
+ stacklevel = 1 ,
1007
+ )
969
1008
self .session .send (socket , "shutdown_reply" , content , parent , ident = ident )
970
1009
# same content, but different msg_id for broadcasting on IOPub
971
1010
self ._shutdown_message = self .session .msg ("shutdown_reply" , content , parent )
@@ -974,7 +1013,7 @@ async def shutdown_request(self, socket, ident, parent):
974
1013
975
1014
self .stop ()
976
1015
977
- def do_shutdown (self , restart ):
1016
+ async def do_shutdown (self , restart ):
978
1017
"""Override in subclasses to do things when the frontend shuts down the
979
1018
kernel.
980
1019
"""
@@ -990,6 +1029,12 @@ async def is_complete_request(self, socket, ident, parent):
990
1029
reply_content = self .do_is_complete (code )
991
1030
if inspect .isawaitable (reply_content ):
992
1031
reply_content = await reply_content
1032
+ else :
1033
+ warnings .warn (
1034
+ _AWAITABLE_MESSAGE .format (func_name = "do_execute" ),
1035
+ PendingDeprecationWarning ,
1036
+ stacklevel = 1 ,
1037
+ )
993
1038
reply_content = json_clean (reply_content )
994
1039
reply_msg = self .session .send (socket , "is_complete_reply" , reply_content , parent , ident )
995
1040
self .log .debug ("%s" , reply_msg )
@@ -1006,6 +1051,12 @@ async def debug_request(self, socket, ident, parent):
1006
1051
reply_content = self .do_debug_request (content )
1007
1052
if inspect .isawaitable (reply_content ):
1008
1053
reply_content = await reply_content
1054
+ else :
1055
+ warnings .warn (
1056
+ _AWAITABLE_MESSAGE .format (func_name = "debug_request" ),
1057
+ PendingDeprecationWarning ,
1058
+ stacklevel = 1 ,
1059
+ )
1009
1060
reply_content = json_clean (reply_content )
1010
1061
reply_msg = self .session .send (socket , "debug_reply" , reply_content , parent , ident )
1011
1062
self .log .debug ("%s" , reply_msg )
0 commit comments