|
1 | 1 | """The IPython kernel implementation"""
|
2 | 2 |
|
| 3 | +from __future__ import annotations |
| 4 | + |
3 | 5 | import builtins
|
4 | 6 | import gc
|
5 | 7 | import getpass
|
|
16 | 18 | from IPython.core import release
|
17 | 19 | from IPython.utils.tokenutil import line_at_cursor, token_at_cursor
|
18 | 20 | from jupyter_client.session import extract_header
|
19 |
| -from traitlets import Any, Bool, HasTraits, Instance, List, Type, observe, observe_compat |
| 21 | +from traitlets import Any, Bool, HasTraits, Instance, List, Type, default, observe, observe_compat |
20 | 22 |
|
21 | 23 | from .comm.comm import BaseComm
|
22 | 24 | from .comm.manager import CommManager
|
@@ -46,7 +48,7 @@ def _create_comm(*args, **kwargs):
|
46 | 48 |
|
47 | 49 | # there can only be one comm manager in a ipykernel process
|
48 | 50 | _comm_lock = threading.Lock()
|
49 |
| -_comm_manager: t.Optional[CommManager] = None |
| 51 | +_comm_manager: CommManager | None = None |
50 | 52 |
|
51 | 53 |
|
52 | 54 | def _get_comm_manager(*args, **kwargs):
|
@@ -84,7 +86,11 @@ def _user_module_changed(self, change):
|
84 | 86 | if self.shell is not None:
|
85 | 87 | self.shell.user_module = change["new"]
|
86 | 88 |
|
87 |
| - user_ns = Instance(dict, args=None, allow_none=True) |
| 89 | + user_ns = Instance("collections.abc.Mapping", allow_none=True) |
| 90 | + |
| 91 | + @default("user_ns") |
| 92 | + def _default_user_ns(self): |
| 93 | + return dict() |
88 | 94 |
|
89 | 95 | @observe("user_ns")
|
90 | 96 | @observe_compat
|
@@ -353,7 +359,7 @@ async def do_execute(
|
353 | 359 |
|
354 | 360 | self._forward_input(allow_stdin)
|
355 | 361 |
|
356 |
| - reply_content: t.Dict[str, t.Any] = {} |
| 362 | + reply_content: dict[str, t.Any] = {} |
357 | 363 | if hasattr(shell, "run_cell_async") and hasattr(shell, "should_run_async"):
|
358 | 364 | run_cell = shell.run_cell_async
|
359 | 365 | should_run_async = shell.should_run_async
|
@@ -559,7 +565,7 @@ def do_inspect(self, code, cursor_pos, detail_level=0, omit_sections=()):
|
559 | 565 | """Handle code inspection."""
|
560 | 566 | name = token_at_cursor(code, cursor_pos)
|
561 | 567 |
|
562 |
| - reply_content: t.Dict[str, t.Any] = {"status": "ok"} |
| 568 | + reply_content: dict[str, t.Any] = {"status": "ok"} |
563 | 569 | reply_content["data"] = {}
|
564 | 570 | reply_content["metadata"] = {}
|
565 | 571 | assert self.shell is not None
|
@@ -755,7 +761,7 @@ def init_closure(self: threading.Thread, *args, **kwargs):
|
755 | 761 | threading.Thread.run = run_closure # type:ignore[method-assign]
|
756 | 762 |
|
757 | 763 | def _clean_thread_parent_frames(
|
758 |
| - self, phase: t.Literal["start", "stop"], info: t.Dict[str, t.Any] |
| 764 | + self, phase: t.Literal["start", "stop"], info: dict[str, t.Any] |
759 | 765 | ):
|
760 | 766 | """Clean parent frames of threads which are no longer running.
|
761 | 767 | This is meant to be invoked by garbage collector callback hook.
|
|
0 commit comments