1919from traitlets import (
2020 Any ,
2121 Bool ,
22+ Dict ,
2223 DottedObjectName ,
2324 Float ,
2425 Instance ,
@@ -206,7 +207,7 @@ def ipykernel(self) -> bool:
206207 return self .kernel_name in {"python" , "python2" , "python3" }
207208
208209 # Protected traits
209- _launch_args : Any = Any ( )
210+ _launch_args : t . Optional [ "Dict[str, Any]" ] = Dict ( allow_none = True )
210211 _control_socket : Any = Any ()
211212
212213 _restarter : Any = Any ()
@@ -281,7 +282,13 @@ def update_env(self, *, env: t.Dict[str, str]) -> None:
281282
282283 .. version-added: 8.5
283284 """
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]
285292
286293 def format_kernel_cmd (self , extra_arguments : t .Optional [t .List [str ]] = None ) -> t .List [str ]:
287294 """Replace templated args (e.g. {connection_file})"""
@@ -307,13 +314,14 @@ def format_kernel_cmd(self, extra_arguments: t.Optional[t.List[str]] = None) ->
307314 # is not usable by non python kernels because the path is being rerouted when
308315 # inside of a store app.
309316 # See this bug here: https://bugs.python.org/issue41196
310- ns = {
317+ ns : t . Dict [ str , t . Any ] = {
311318 "connection_file" : os .path .realpath (self .connection_file ),
312319 "prefix" : sys .prefix ,
313320 }
314321
315322 if self .kernel_spec : # type:ignore[truthy-bool]
316323 ns ["resource_dir" ] = self .kernel_spec .resource_dir
324+ assert isinstance (self ._launch_args , dict )
317325
318326 ns .update (self ._launch_args )
319327
@@ -371,7 +379,8 @@ async def _async_pre_start_kernel(
371379 self .shutting_down = False
372380 self .kernel_id = self .kernel_id or kw .pop ("kernel_id" , str (uuid .uuid4 ()))
373381 # 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]
375384 if self .provisioner is None : # will not be None on restarts
376385 self .provisioner = KPF .instance (parent = self .parent ).create_provisioner_instance (
377386 self .kernel_id ,
0 commit comments