1+ from __future__ import annotations
2+
13import contextlib
24import sys
35import uuid
46from itertools import chain
57from pathlib import Path
8+ from typing import cast
69
710from IPython .display import SVG , Image
811from metakernel import MetaKernel , ProcessMetaKernel , pexpect
@@ -25,8 +28,8 @@ class GnuplotKernel(ProcessMetaKernel):
2528 implementation = "Gnuplot Kernel"
2629 implementation_version = get_version ("gnuplot_kernel" )
2730 language = "gnuplot"
28- language_version = "5.0 "
29- banner = "Gnuplot Kernel"
31+ _banner = "Gnuplot Kernel "
32+ language_version = "5.0" # pyright: ignore[reportAssignmentType,reportIncompatibleMethodOverride]
3033 language_info = {
3134 "mimetype" : "text/x-gnuplot" ,
3235 "name" : "gnuplot" ,
@@ -50,20 +53,20 @@ class GnuplotKernel(ProcessMetaKernel):
5053 inline_plotting = True
5154 reset_code = ""
5255 _first = True
53- _image_files = []
56+ _image_files : list [ Path ] = []
5457 _error = False
5558
59+ wrapper : GnuplotREPLWrapper
60+
5661 def bad_prompt_warning (self ):
5762 """
5863 Print warning if the prompt is not 'gnuplot>'
5964 """
60- if not self .wrapper .prompt .startswith ("gnuplot>" ):
61- msg = "Warning: The prompt is currently set to '{}'" .format (
62- self .wrapper .prompt
63- )
64- print (msg )
65+ prompt = cast ("str" , self .wrapper .prompt ).strip ()
66+ if not prompt .endswith ("gnuplot>" ):
67+ print (f"Warning: The prompt is currently set to '{ prompt } '" )
6568
66- def do_execute_direct (self , code ):
69+ def do_execute_direct (self , code , silent = False ):
6770 # We wrap the real function so that gnuplot_kernel can
6871 # give a message when an exception occurs. Without
6972 # this, an exception happens silently
@@ -73,7 +76,7 @@ def do_execute_direct(self, code):
7376 print (f"Error: { err } " )
7477 raise err
7578
76- def _do_execute_direct (self , code ) :
79+ def _do_execute_direct (self , code : str ) -> TextOutput | None :
7780 """
7881 Execute gnuplot code
7982 """
@@ -105,7 +108,7 @@ def _do_execute_direct(self, code):
105108 # No empty strings
106109 return result if (result and result .output ) else None
107110
108- def add_inline_image_statements (self , code ) :
111+ def add_inline_image_statements (self , code : str ) -> str :
109112 """
110113 Add 'set output ...' before every plotting statement
111114
@@ -188,6 +191,8 @@ def display_images(self):
188191 settings = self .plot_settings
189192 if self .inline_plotting :
190193 _Image = SVG if settings ["format" ] == "svg" else Image
194+ else :
195+ return
191196
192197 for filename in self .iter_image_files ():
193198 try :
@@ -238,12 +243,11 @@ def makeWrapper(self):
238243 else :
239244 command = program
240245
241- d = dict (
246+ wrapper = GnuplotREPLWrapper (
242247 cmd_or_spawn = command ,
243248 prompt_regex = PROMPT_RE ,
244249 prompt_change_cmd = None ,
245250 )
246- wrapper = GnuplotREPLWrapper (** d )
247251 # No sleeping before sending commands to gnuplot
248252 wrapper .child .delaybeforesend = 0
249253 return wrapper
@@ -258,11 +262,8 @@ def do_shutdown(self, restart):
258262 def get_kernel_help_on (self , info , level = 0 , none_on_fail = False ):
259263 obj = info .get ("help_obj" , "" )
260264 if not obj or len (obj .split ()) > 1 :
261- if none_on_fail :
262- return None
263- else :
264- return ""
265- res = self .do_execute_direct ("help %s" % obj )
265+ return None if none_on_fail else ""
266+ res = cast ("TextOutput" , self .do_execute_direct ("help %s" % obj ))
266267 text = PROMPT_REMOVE_RE .sub ("" , res .output )
267268 self .bad_prompt_warning ()
268269 return text
0 commit comments