From 84498149e7ae17e2afc411655022b1dc3a1798ca Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Sep 2023 18:02:43 +0100 Subject: [PATCH 1/4] fix: remove warnings for 3.11+ --- myst_nb/sphinx_ext.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/myst_nb/sphinx_ext.py b/myst_nb/sphinx_ext.py index 4e821623..83b23b5d 100644 --- a/myst_nb/sphinx_ext.py +++ b/myst_nb/sphinx_ext.py @@ -5,6 +5,7 @@ from importlib import resources as import_resources import os from pathlib import Path +import sys from typing import Any from myst_parser.sphinx_ext.main import setup_sphinx as setup_myst_parser @@ -184,9 +185,20 @@ def _get_file_hash(path: Path): return hashlib.sha256(path.read_bytes()).hexdigest() +def _import_resources_path(package, resource): + if sys.version_info < (3, 9): + with import_resources.path(package, resource) as path: + yield path + else: + with import_resources.as_file( + import_resources.files(package).joinpath(resource) + ) as path: + yield path + + def add_css(app: Sphinx): """Add CSS for myst-nb.""" - with import_resources.path(static, "mystnb.css") as source_path: + with _import_resources_path(static, "mystnb.css") as source_path: hash = _get_file_hash(source_path) app.add_css_file(f"mystnb.{hash}.css") @@ -195,9 +207,8 @@ def add_global_html_resources(app: Sphinx, exception): """Add HTML resources that apply to all pages.""" # see https://github.com/sphinx-doc/sphinx/issues/1379 if app.builder is not None and app.builder.format == "html" and not exception: - with import_resources.path(static, "mystnb.css") as source_path: - with import_resources.path(static, "mystnb.css") as source_path: - hash = _get_file_hash(source_path) + with _import_resources_path(static, "mystnb.css") as source_path: + hash = _get_file_hash(source_path) destination = os.path.join( app.builder.outdir, "_static", f"mystnb.{hash}.css" ) @@ -210,6 +221,6 @@ def add_per_page_html_resources( """Add JS files for this page, identified from the parsing of the notebook.""" if app.env is None or app.builder is None or app.builder.format != "html": return - js_files = NbMetadataCollector.get_js_files(app.env, pagename) # type: ignore + js_files = NbMetadataCollector.get_js_files(app.env, pagename) for path, kwargs in js_files.values(): - app.add_js_file(path, **kwargs) # type: ignore + app.add_js_file(path, **kwargs) From 3ac102c79a4e6f744484ca399e130f2da540d82b Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Sep 2023 18:07:48 +0100 Subject: [PATCH 2/4] ci: use latest deps for mypy --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2bfa3efc..56dfe201 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,8 +48,8 @@ repos: args: [--config-file=pyproject.toml] additional_dependencies: - importlib_metadata - - myst-parser~=1.0.0 - - "sphinx~=5.0" + - myst-parser~=2.0.0 + - "sphinx~=7.0" - nbclient - types-PyYAML files: > From f6646eb6d5d5618743f520e7e1a8cc12a6a0a7dd Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Sep 2023 18:39:02 +0100 Subject: [PATCH 3/4] fix: use context-manager decorator --- myst_nb/sphinx_ext.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/myst_nb/sphinx_ext.py b/myst_nb/sphinx_ext.py index 83b23b5d..1044e457 100644 --- a/myst_nb/sphinx_ext.py +++ b/myst_nb/sphinx_ext.py @@ -1,12 +1,14 @@ """Setup for the myst-nb sphinx extension.""" from __future__ import annotations +import contextlib import hashlib from importlib import resources as import_resources import os from pathlib import Path import sys -from typing import Any +from types import ModuleType +from typing import Any, Iterator from myst_parser.sphinx_ext.main import setup_sphinx as setup_myst_parser from sphinx.application import Sphinx @@ -185,7 +187,8 @@ def _get_file_hash(path: Path): return hashlib.sha256(path.read_bytes()).hexdigest() -def _import_resources_path(package, resource): +@contextlib.contextmanager +def _import_resources_path(package: ModuleType, resource: str) -> Iterator[Path]: if sys.version_info < (3, 9): with import_resources.path(package, resource) as path: yield path From 02b05cdf500938535ed8d432f635e25a102d990a Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Sep 2023 18:48:13 +0100 Subject: [PATCH 4/4] chore: type-ignore --- myst_nb/sphinx_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/myst_nb/sphinx_.py b/myst_nb/sphinx_.py index 6bf07fa8..f6c550f3 100644 --- a/myst_nb/sphinx_.py +++ b/myst_nb/sphinx_.py @@ -140,7 +140,7 @@ def parse(self, inputstring: str, document: nodes.document) -> None: # so that roles/directives can access it document.attributes["nb_renderer"] = nb_renderer # we currently do this early, so that the nb_renderer has access to things - mdit_renderer.setup_render(mdit_parser.options, mdit_env) + mdit_renderer.setup_render(mdit_parser.options, mdit_env) # type: ignore # parse notebook structure to markdown-it tokens # note, this does not assume that the notebook has been executed yet