Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: remove warnings for 3.11+ #547

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
2 changes: 1 addition & 1 deletion myst_nb/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions myst_nb/sphinx_ext.py
Original file line number Diff line number Diff line change
@@ -1,11 +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
from typing import Any
import sys
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
Expand Down Expand Up @@ -184,9 +187,21 @@ def _get_file_hash(path: Path):
return hashlib.sha256(path.read_bytes()).hexdigest()


@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
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")

Expand All @@ -195,9 +210,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"
)
Expand All @@ -210,6 +224,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)