57
57
from ._version import kernel_protocol_version
58
58
from .iostream import OutStream
59
59
60
+ _AWAITABLE_MESSAGE : str = (
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). It might become a requirement in the future."
64
+ " Coroutine functions and awaitables have been supported since"
65
+ " ipykernel 6.0 (2021)."
66
+ )
67
+
60
68
61
69
def _accepts_parameters (meth , param_names ):
62
70
parameters = inspect .signature (meth ).parameters
@@ -742,6 +750,12 @@ async def execute_request(self, socket, ident, parent):
742
750
743
751
if inspect .isawaitable (reply_content ):
744
752
reply_content = await reply_content
753
+ else :
754
+ warnings .warn (
755
+ _AWAITABLE_MESSAGE .format (func_name = "execute_request" ),
756
+ PendingDeprecationWarning ,
757
+ stacklevel = 1 ,
758
+ )
745
759
746
760
# Flush output before sending the reply.
747
761
if sys .stdout is not None :
@@ -802,12 +816,27 @@ async def complete_request(self, socket, ident, parent):
802
816
matches = self .do_complete (code , cursor_pos )
803
817
if inspect .isawaitable (matches ):
804
818
matches = await matches
819
+ else :
820
+ warnings .warn (
821
+ _AWAITABLE_MESSAGE .format (func_name = "do_complete" ),
822
+ PendingDeprecationWarning ,
823
+ stacklevel = 1 ,
824
+ )
805
825
806
826
matches = json_clean (matches )
807
827
self .session .send (socket , "complete_reply" , matches , parent , ident )
808
828
809
829
def do_complete (self , code , cursor_pos ):
810
- """Override in subclasses to find completions."""
830
+ """Override in subclasses to find completions.
831
+
832
+ .. note::
833
+
834
+ Subclass should likely make this method return an awaitable,
835
+ but the base class method will need to stay sync for a few versions
836
+ to not break spyder-kernel and qtconsole.
837
+
838
+
839
+ """
811
840
return {
812
841
"matches" : [],
813
842
"cursor_end" : cursor_pos ,
@@ -830,6 +859,12 @@ async def inspect_request(self, socket, ident, parent):
830
859
)
831
860
if inspect .isawaitable (reply_content ):
832
861
reply_content = await reply_content
862
+ else :
863
+ warnings .warn (
864
+ _AWAITABLE_MESSAGE .format (func_name = "inspect_request" ),
865
+ PendingDeprecationWarning ,
866
+ stacklevel = 1 ,
867
+ )
833
868
834
869
# Before we send this object over, we scrub it for JSON usage
835
870
reply_content = json_clean (reply_content )
@@ -849,6 +884,12 @@ async def history_request(self, socket, ident, parent):
849
884
reply_content = self .do_history (** content )
850
885
if inspect .isawaitable (reply_content ):
851
886
reply_content = await reply_content
887
+ else :
888
+ warnings .warn (
889
+ _AWAITABLE_MESSAGE .format (func_name = "history_request" ),
890
+ PendingDeprecationWarning ,
891
+ stacklevel = 1 ,
892
+ )
852
893
853
894
reply_content = json_clean (reply_content )
854
895
msg = self .session .send (socket , "history_reply" , reply_content , parent , ident )
@@ -966,6 +1007,12 @@ async def shutdown_request(self, socket, ident, parent):
966
1007
content = self .do_shutdown (parent ["content" ]["restart" ])
967
1008
if inspect .isawaitable (content ):
968
1009
content = await content
1010
+ else :
1011
+ warnings .warn (
1012
+ _AWAITABLE_MESSAGE .format (func_name = "do_shutdown" ),
1013
+ PendingDeprecationWarning ,
1014
+ stacklevel = 1 ,
1015
+ )
969
1016
self .session .send (socket , "shutdown_reply" , content , parent , ident = ident )
970
1017
# same content, but different msg_id for broadcasting on IOPub
971
1018
self ._shutdown_message = self .session .msg ("shutdown_reply" , content , parent )
@@ -977,6 +1024,12 @@ async def shutdown_request(self, socket, ident, parent):
977
1024
def do_shutdown (self , restart ):
978
1025
"""Override in subclasses to do things when the frontend shuts down the
979
1026
kernel.
1027
+
1028
+ .. note::
1029
+
1030
+ Subclass should likely make this method return an awaitable,
1031
+ but the base class method will need to stay sync for a few versions
1032
+ to not break spyder-kernel and qtconsole.
980
1033
"""
981
1034
return {"status" : "ok" , "restart" : restart }
982
1035
@@ -990,6 +1043,12 @@ async def is_complete_request(self, socket, ident, parent):
990
1043
reply_content = self .do_is_complete (code )
991
1044
if inspect .isawaitable (reply_content ):
992
1045
reply_content = await reply_content
1046
+ else :
1047
+ warnings .warn (
1048
+ _AWAITABLE_MESSAGE .format (func_name = "do_execute" ),
1049
+ PendingDeprecationWarning ,
1050
+ stacklevel = 1 ,
1051
+ )
993
1052
reply_content = json_clean (reply_content )
994
1053
reply_msg = self .session .send (socket , "is_complete_reply" , reply_content , parent , ident )
995
1054
self .log .debug ("%s" , reply_msg )
@@ -1006,6 +1065,12 @@ async def debug_request(self, socket, ident, parent):
1006
1065
reply_content = self .do_debug_request (content )
1007
1066
if inspect .isawaitable (reply_content ):
1008
1067
reply_content = await reply_content
1068
+ else :
1069
+ warnings .warn (
1070
+ _AWAITABLE_MESSAGE .format (func_name = "debug_request" ),
1071
+ PendingDeprecationWarning ,
1072
+ stacklevel = 1 ,
1073
+ )
1009
1074
reply_content = json_clean (reply_content )
1010
1075
reply_msg = self .session .send (socket , "debug_reply" , reply_content , parent , ident )
1011
1076
self .log .debug ("%s" , reply_msg )
0 commit comments