19
19
from traitlets import (
20
20
Any ,
21
21
Bool ,
22
+ Dict ,
22
23
DottedObjectName ,
23
24
Float ,
24
25
Instance ,
@@ -206,7 +207,7 @@ def ipykernel(self) -> bool:
206
207
return self .kernel_name in {"python" , "python2" , "python3" }
207
208
208
209
# Protected traits
209
- _launch_args : Any = Any ( )
210
+ _launch_args : t . Optional [ "Dict[str, Any]" ] = Dict ( allow_none = True )
210
211
_control_socket : Any = Any ()
211
212
212
213
_restarter : Any = Any ()
@@ -281,7 +282,13 @@ def update_env(self, *, env: t.Dict[str, str]) -> None:
281
282
282
283
.. version-added: 8.5
283
284
"""
284
- self ._launch_args ["env" ].update (env )
285
+ # Mypy think this is unreachable as it see _launch_args as Dict, not t.Dict
286
+ if (
287
+ isinstance (self ._launch_args , dict )
288
+ and "env" in self ._launch_args
289
+ and isinstance (self ._launch_args ["env" ], dict ) # type: ignore [unreachable]
290
+ ):
291
+ self ._launch_args ["env" ].update (env ) # type: ignore [unreachable]
285
292
286
293
def format_kernel_cmd (self , extra_arguments : t .Optional [t .List [str ]] = None ) -> t .List [str ]:
287
294
"""Replace templated args (e.g. {connection_file})"""
@@ -307,13 +314,14 @@ def format_kernel_cmd(self, extra_arguments: t.Optional[t.List[str]] = None) ->
307
314
# is not usable by non python kernels because the path is being rerouted when
308
315
# inside of a store app.
309
316
# See this bug here: https://bugs.python.org/issue41196
310
- ns = {
317
+ ns : t . Dict [ str , t . Any ] = {
311
318
"connection_file" : os .path .realpath (self .connection_file ),
312
319
"prefix" : sys .prefix ,
313
320
}
314
321
315
322
if self .kernel_spec : # type:ignore[truthy-bool]
316
323
ns ["resource_dir" ] = self .kernel_spec .resource_dir
324
+ assert isinstance (self ._launch_args , dict )
317
325
318
326
ns .update (self ._launch_args )
319
327
@@ -371,7 +379,8 @@ async def _async_pre_start_kernel(
371
379
self .shutting_down = False
372
380
self .kernel_id = self .kernel_id or kw .pop ("kernel_id" , str (uuid .uuid4 ()))
373
381
# save kwargs for use in restart
374
- self ._launch_args = kw .copy ()
382
+ # assigning Traitlets Dicts to Dict make mypy unhappy but is ok
383
+ self ._launch_args = kw .copy () # type:ignore [assignment]
375
384
if self .provisioner is None : # will not be None on restarts
376
385
self .provisioner = KPF .instance (parent = self .parent ).create_provisioner_instance (
377
386
self .kernel_id ,
0 commit comments