12
12
logger = logging .getLogger (__name__ )
13
13
14
14
if 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
16
22
17
23
CLIShellLiteral : TypeAlias = t .Literal [
18
24
"best" , "pdb" , "code" , "ptipython" , "ptpython" , "ipython" , "bpython"
19
25
]
20
26
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
+
21
44
22
45
def has_ipython () -> bool :
23
46
try :
@@ -77,13 +100,15 @@ def detect_best_shell() -> "CLIShellLiteral":
77
100
return "code"
78
101
79
102
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 ]:
81
106
if extra_args is None :
82
107
extra_args = {}
83
108
84
109
from bpython import embed # F841
85
110
86
- def launch_bpython ():
111
+ def launch_bpython () -> None :
87
112
imported_objects = get_launch_args (** options )
88
113
kwargs = {}
89
114
if extra_args :
@@ -93,16 +118,18 @@ def launch_bpython():
93
118
return launch_bpython
94
119
95
120
96
- def get_ipython_arguments ():
121
+ def get_ipython_arguments () -> t . List [ str ] :
97
122
ipython_args = "IPYTHON_ARGUMENTS"
98
123
return os .environ .get (ipython_args , "" ).split ()
99
124
100
125
101
- def get_ipython (options , ** extra_args ):
126
+ def get_ipython (
127
+ options : "LaunchOptionalImports" , ** extra_args : t .Dict [str , t .Any ]
128
+ ) -> t .Any :
102
129
try :
103
130
from IPython import start_ipython
104
131
105
- def launch_ipython ():
132
+ def launch_ipython () -> None :
106
133
imported_objects = get_launch_args (** options )
107
134
ipython_arguments = extra_args or get_ipython_arguments ()
108
135
start_ipython (argv = ipython_arguments , user_ns = imported_objects )
@@ -115,21 +142,21 @@ def launch_ipython():
115
142
# Notebook not supported for IPython < 0.11.
116
143
from IPython .Shell import IPShell
117
144
118
- def launch_ipython ():
145
+ def launch_ipython () -> None :
119
146
imported_objects = get_launch_args (** options )
120
147
shell = IPShell (argv = [], user_ns = imported_objects )
121
148
shell .mainloop ()
122
149
123
150
return launch_ipython
124
151
125
152
126
- def get_ptpython (options , vi_mode = False ):
153
+ def get_ptpython (options : "LaunchOptionalImports" , vi_mode : bool = False ) -> t . Any :
127
154
try :
128
155
from ptpython .repl import embed , run_config
129
156
except ImportError :
130
157
from prompt_toolkit .contrib .repl import embed , run_config
131
158
132
- def launch_ptpython ():
159
+ def launch_ptpython () -> None :
133
160
imported_objects = get_launch_args (** options )
134
161
history_filename = str (pathlib .Path ("~/.ptpython_history" ).expanduser ())
135
162
embed (
@@ -142,7 +169,7 @@ def launch_ptpython():
142
169
return launch_ptpython
143
170
144
171
145
- def get_ptipython (options , vi_mode = False ):
172
+ def get_ptipython (options : "LaunchOptionalImports" , vi_mode : bool = False ) -> t . Any :
146
173
"""Based on django-extensions
147
174
148
175
Run renamed to launch, get_imported_objects renamed to get_launch_args
@@ -155,7 +182,7 @@ def get_ptipython(options, vi_mode=False):
155
182
from prompt_toolkit .contrib .ipython import embed
156
183
from prompt_toolkit .contrib .repl import run_config
157
184
158
- def launch_ptipython ():
185
+ def launch_ptipython () -> None :
159
186
imported_objects = get_launch_args (** options )
160
187
history_filename = str (pathlib .Path ("~/.ptpython_history" ).expanduser ())
161
188
embed (
@@ -168,7 +195,7 @@ def launch_ptipython():
168
195
return launch_ptipython
169
196
170
197
171
- def get_launch_args (** kwargs ) :
198
+ def get_launch_args (** kwargs : "Unpack[LaunchOptionalImports]" ) -> "LaunchImports" :
172
199
import libtmux
173
200
from libtmux .pane import Pane
174
201
from libtmux .server import Server
@@ -188,7 +215,7 @@ def get_launch_args(**kwargs):
188
215
}
189
216
190
217
191
- def get_code (use_pythonrc , imported_objects ) :
218
+ def get_code (use_pythonrc : bool , imported_objects : "LaunchImports" ) -> t . Any :
192
219
import code
193
220
194
221
try :
@@ -201,7 +228,11 @@ def get_code(use_pythonrc, imported_objects):
201
228
# we already know 'readline' was imported successfully.
202
229
import rlcompleter
203
230
204
- readline .set_completer (rlcompleter .Completer (imported_objects ).complete )
231
+ readline .set_completer (
232
+ rlcompleter .Completer (
233
+ imported_objects , # type:ignore
234
+ ).complete
235
+ )
205
236
# Enable tab completion on systems using libedit (e.g. macOS).
206
237
# These lines are copied from Lib/site.py on Python 3.4.
207
238
readline_doc = getattr (readline , "__doc__" , "" )
@@ -226,9 +257,12 @@ def get_code(use_pythonrc, imported_objects):
226
257
pythonrc_code = handle .read ()
227
258
# Match the behavior of the cpython shell where an error in
228
259
# 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
+ )
230
264
231
- def launch_code ():
265
+ def launch_code () -> None :
232
266
code .interact (local = imported_objects )
233
267
234
268
return launch_code
@@ -238,7 +272,7 @@ def launch(
238
272
shell : t .Optional ["CLIShellLiteral" ] = "best" ,
239
273
use_pythonrc : bool = False ,
240
274
use_vi_mode : bool = False ,
241
- ** kwargs
275
+ ** kwargs : "Unpack[LaunchOptionalImports]"
242
276
) -> None :
243
277
# Also allowing passing shell='code' to force using code.interact
244
278
imported_objects = get_launch_args (** kwargs )
0 commit comments