1212logger = logging .getLogger (__name__ )
1313
1414if t .TYPE_CHECKING :
15- from typing_extensions import TypeAlias
15+ from types import ModuleType
16+
17+ from libtmux .pane import Pane
18+ from libtmux .server import Server
19+ from libtmux .session import Session
20+ from libtmux .window import Window
21+ from typing_extensions import NotRequired , TypeAlias , TypedDict , Unpack
1622
1723 CLIShellLiteral : TypeAlias = t .Literal [
1824 "best" , "pdb" , "code" , "ptipython" , "ptpython" , "ipython" , "bpython"
1925 ]
2026
27+ class LaunchOptionalImports (TypedDict ):
28+ server : NotRequired ["Server" ]
29+ session : NotRequired ["Session" ]
30+ window : NotRequired ["Window" ]
31+ pane : NotRequired ["Pane" ]
32+
33+ class LaunchImports (t .TypedDict ):
34+ libtmux : ModuleType
35+ Server : t .Type [Server ]
36+ Session : t .Type [Session ]
37+ Window : t .Type [Window ]
38+ Pane : t .Type [Pane ]
39+ server : t .Optional ["Server" ]
40+ session : t .Optional ["Session" ]
41+ window : t .Optional ["Window" ]
42+ pane : t .Optional ["Pane" ]
43+
2144
2245def has_ipython () -> bool :
2346 try :
@@ -77,13 +100,15 @@ def detect_best_shell() -> "CLIShellLiteral":
77100 return "code"
78101
79102
80- def get_bpython (options , extra_args = None ):
103+ def get_bpython (
104+ options : "LaunchOptionalImports" , extra_args : t .Optional [t .Dict [str , t .Any ]] = None
105+ ) -> t .Callable [[], None ]:
81106 if extra_args is None :
82107 extra_args = {}
83108
84109 from bpython import embed # F841
85110
86- def launch_bpython ():
111+ def launch_bpython () -> None :
87112 imported_objects = get_launch_args (** options )
88113 kwargs = {}
89114 if extra_args :
@@ -93,16 +118,18 @@ def launch_bpython():
93118 return launch_bpython
94119
95120
96- def get_ipython_arguments ():
121+ def get_ipython_arguments () -> t . List [ str ] :
97122 ipython_args = "IPYTHON_ARGUMENTS"
98123 return os .environ .get (ipython_args , "" ).split ()
99124
100125
101- def get_ipython (options , ** extra_args ):
126+ def get_ipython (
127+ options : "LaunchOptionalImports" , ** extra_args : t .Dict [str , t .Any ]
128+ ) -> t .Any :
102129 try :
103130 from IPython import start_ipython
104131
105- def launch_ipython ():
132+ def launch_ipython () -> None :
106133 imported_objects = get_launch_args (** options )
107134 ipython_arguments = extra_args or get_ipython_arguments ()
108135 start_ipython (argv = ipython_arguments , user_ns = imported_objects )
@@ -115,21 +142,21 @@ def launch_ipython():
115142 # Notebook not supported for IPython < 0.11.
116143 from IPython .Shell import IPShell
117144
118- def launch_ipython ():
145+ def launch_ipython () -> None :
119146 imported_objects = get_launch_args (** options )
120147 shell = IPShell (argv = [], user_ns = imported_objects )
121148 shell .mainloop ()
122149
123150 return launch_ipython
124151
125152
126- def get_ptpython (options , vi_mode = False ):
153+ def get_ptpython (options : "LaunchOptionalImports" , vi_mode : bool = False ) -> t . Any :
127154 try :
128155 from ptpython .repl import embed , run_config
129156 except ImportError :
130157 from prompt_toolkit .contrib .repl import embed , run_config
131158
132- def launch_ptpython ():
159+ def launch_ptpython () -> None :
133160 imported_objects = get_launch_args (** options )
134161 history_filename = str (pathlib .Path ("~/.ptpython_history" ).expanduser ())
135162 embed (
@@ -142,7 +169,7 @@ def launch_ptpython():
142169 return launch_ptpython
143170
144171
145- def get_ptipython (options , vi_mode = False ):
172+ def get_ptipython (options : "LaunchOptionalImports" , vi_mode : bool = False ) -> t . Any :
146173 """Based on django-extensions
147174
148175 Run renamed to launch, get_imported_objects renamed to get_launch_args
@@ -155,7 +182,7 @@ def get_ptipython(options, vi_mode=False):
155182 from prompt_toolkit .contrib .ipython import embed
156183 from prompt_toolkit .contrib .repl import run_config
157184
158- def launch_ptipython ():
185+ def launch_ptipython () -> None :
159186 imported_objects = get_launch_args (** options )
160187 history_filename = str (pathlib .Path ("~/.ptpython_history" ).expanduser ())
161188 embed (
@@ -168,7 +195,7 @@ def launch_ptipython():
168195 return launch_ptipython
169196
170197
171- def get_launch_args (** kwargs ) :
198+ def get_launch_args (** kwargs : "Unpack[LaunchOptionalImports]" ) -> "LaunchImports" :
172199 import libtmux
173200 from libtmux .pane import Pane
174201 from libtmux .server import Server
@@ -188,7 +215,7 @@ def get_launch_args(**kwargs):
188215 }
189216
190217
191- def get_code (use_pythonrc , imported_objects ) :
218+ def get_code (use_pythonrc : bool , imported_objects : "LaunchImports" ) -> t . Any :
192219 import code
193220
194221 try :
@@ -201,7 +228,11 @@ def get_code(use_pythonrc, imported_objects):
201228 # we already know 'readline' was imported successfully.
202229 import rlcompleter
203230
204- readline .set_completer (rlcompleter .Completer (imported_objects ).complete )
231+ readline .set_completer (
232+ rlcompleter .Completer (
233+ imported_objects , # type:ignore
234+ ).complete
235+ )
205236 # Enable tab completion on systems using libedit (e.g. macOS).
206237 # These lines are copied from Lib/site.py on Python 3.4.
207238 readline_doc = getattr (readline , "__doc__" , "" )
@@ -226,9 +257,12 @@ def get_code(use_pythonrc, imported_objects):
226257 pythonrc_code = handle .read ()
227258 # Match the behavior of the cpython shell where an error in
228259 # PYTHONSTARTUP prints an exception and continues.
229- exec (compile (pythonrc_code , pythonrc , "exec" ), imported_objects )
260+ exec (
261+ compile (pythonrc_code , pythonrc , "exec" ),
262+ imported_objects , # type:ignore
263+ )
230264
231- def launch_code ():
265+ def launch_code () -> None :
232266 code .interact (local = imported_objects )
233267
234268 return launch_code
@@ -238,7 +272,7 @@ def launch(
238272 shell : t .Optional ["CLIShellLiteral" ] = "best" ,
239273 use_pythonrc : bool = False ,
240274 use_vi_mode : bool = False ,
241- ** kwargs
275+ ** kwargs : "Unpack[LaunchOptionalImports]"
242276) -> None :
243277 # Also allowing passing shell='code' to force using code.interact
244278 imported_objects = get_launch_args (** kwargs )
0 commit comments