From 3b353d7f59bc44ee85bce3cbd7c07eb032986993 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 26 Sep 2023 20:53:15 -0500 Subject: [PATCH] Fix event loop handling (#362) * Fix event loop handling * update typing --- jupyter_core/application.py | 18 ++++++++---------- jupyter_core/utils/__init__.py | 9 +++++---- pyproject.toml | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/jupyter_core/application.py b/jupyter_core/application.py index 7b65f49..e55a657 100644 --- a/jupyter_core/application.py +++ b/jupyter_core/application.py @@ -83,7 +83,7 @@ def _log_level_default(self) -> int: def _jupyter_path_default(self) -> list[str]: return jupyter_path() - config_dir: str | Unicode = Unicode() + config_dir = Unicode() def _config_dir_default(self) -> str: return jupyter_config_dir() @@ -96,41 +96,39 @@ def config_file_paths(self) -> list[str]: path.insert(0, self.config_dir) return path - data_dir: str | Unicode = Unicode() + data_dir = Unicode() def _data_dir_default(self) -> str: d = jupyter_data_dir() ensure_dir_exists(d, mode=0o700) return d - runtime_dir: str | Unicode = Unicode() + runtime_dir = Unicode() def _runtime_dir_default(self) -> str: rd = jupyter_runtime_dir() ensure_dir_exists(rd, mode=0o700) return rd - @observe("runtime_dir") # type:ignore[misc] + @observe("runtime_dir") def _runtime_dir_changed(self, change: t.Any) -> None: ensure_dir_exists(change["new"], mode=0o700) - generate_config: bool | Bool = Bool( - False, config=True, help="""Generate default config file.""" - ) + generate_config = Bool(False, config=True, help="""Generate default config file.""") - config_file_name: str | Unicode = Unicode(config=True, help="Specify a config file to load.") + config_file_name = Unicode(config=True, help="Specify a config file to load.") def _config_file_name_default(self) -> str: if not self.name: return "" return self.name.replace("-", "_") + "_config" - config_file: str | Unicode = Unicode( + config_file = Unicode( config=True, help="""Full path of a config file.""", ) - answer_yes: bool | Bool = Bool(False, config=True, help="""Answer yes to any prompts.""") + answer_yes = Bool(False, config=True, help="""Answer yes to any prompts.""") def write_default_config(self) -> None: """Write our default config to a .py config file""" diff --git a/jupyter_core/utils/__init__.py b/jupyter_core/utils/__init__.py index 6e26c17..0c043d4 100644 --- a/jupyter_core/utils/__init__.py +++ b/jupyter_core/utils/__init__.py @@ -127,7 +127,6 @@ def run(self, coro: Any) -> Any: _runner_map: dict[str, _TaskRunner] = {} -_loop_map: dict[str, asyncio.AbstractEventLoop] = {} def run_sync(coro: Callable[..., Awaitable[T]]) -> Callable[..., T]: @@ -161,9 +160,11 @@ def wrapped(*args: Any, **kwargs: Any) -> Any: pass # Run the loop for this thread. - if name not in _loop_map: - _loop_map[name] = asyncio.new_event_loop() - loop = _loop_map[name] + try: + loop = asyncio.get_event_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) return loop.run_until_complete(inner) wrapped.__doc__ = coro.__doc__ diff --git a/pyproject.toml b/pyproject.toml index 6deba32..ae05147 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,7 +88,7 @@ nowarn = "test -W default {args}" [tool.hatch.envs.typing] features = ["test"] -dependencies = ["mypy>=1.5.1"] +dependencies = ["mypy>=1.5.1", "traitlets>=5.10.1"] [tool.hatch.envs.typing.scripts] test = "mypy --install-types --non-interactive {args}"