Skip to content

Commit 9ddc821

Browse files
authored
fix: remove warnings for 3.11+ (#547)
* fix: remove warnings for 3.11+ * ci: use latest deps for mypy * fix: use context-manager decorator * chore: type-ignore
1 parent 59854c2 commit 9ddc821

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ repos:
4848
args: [--config-file=pyproject.toml]
4949
additional_dependencies:
5050
- importlib_metadata
51-
- myst-parser~=1.0.0
52-
- "sphinx~=5.0"
51+
- myst-parser~=2.0.0
52+
- "sphinx~=7.0"
5353
- nbclient
5454
- types-PyYAML
5555
files: >

myst_nb/sphinx_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
140140
# so that roles/directives can access it
141141
document.attributes["nb_renderer"] = nb_renderer
142142
# we currently do this early, so that the nb_renderer has access to things
143-
mdit_renderer.setup_render(mdit_parser.options, mdit_env)
143+
mdit_renderer.setup_render(mdit_parser.options, mdit_env) # type: ignore
144144

145145
# parse notebook structure to markdown-it tokens
146146
# note, this does not assume that the notebook has been executed yet

myst_nb/sphinx_ext.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Setup for the myst-nb sphinx extension."""
22
from __future__ import annotations
33

4+
import contextlib
45
import hashlib
56
from importlib import resources as import_resources
67
import os
78
from pathlib import Path
8-
from typing import Any
9+
import sys
10+
from types import ModuleType
11+
from typing import Any, Iterator
912

1013
from myst_parser.sphinx_ext.main import setup_sphinx as setup_myst_parser
1114
from sphinx.application import Sphinx
@@ -184,9 +187,21 @@ def _get_file_hash(path: Path):
184187
return hashlib.sha256(path.read_bytes()).hexdigest()
185188

186189

190+
@contextlib.contextmanager
191+
def _import_resources_path(package: ModuleType, resource: str) -> Iterator[Path]:
192+
if sys.version_info < (3, 9):
193+
with import_resources.path(package, resource) as path:
194+
yield path
195+
else:
196+
with import_resources.as_file(
197+
import_resources.files(package).joinpath(resource)
198+
) as path:
199+
yield path
200+
201+
187202
def add_css(app: Sphinx):
188203
"""Add CSS for myst-nb."""
189-
with import_resources.path(static, "mystnb.css") as source_path:
204+
with _import_resources_path(static, "mystnb.css") as source_path:
190205
hash = _get_file_hash(source_path)
191206
app.add_css_file(f"mystnb.{hash}.css")
192207

@@ -195,9 +210,8 @@ def add_global_html_resources(app: Sphinx, exception):
195210
"""Add HTML resources that apply to all pages."""
196211
# see https://github.com/sphinx-doc/sphinx/issues/1379
197212
if app.builder is not None and app.builder.format == "html" and not exception:
198-
with import_resources.path(static, "mystnb.css") as source_path:
199-
with import_resources.path(static, "mystnb.css") as source_path:
200-
hash = _get_file_hash(source_path)
213+
with _import_resources_path(static, "mystnb.css") as source_path:
214+
hash = _get_file_hash(source_path)
201215
destination = os.path.join(
202216
app.builder.outdir, "_static", f"mystnb.{hash}.css"
203217
)
@@ -210,6 +224,6 @@ def add_per_page_html_resources(
210224
"""Add JS files for this page, identified from the parsing of the notebook."""
211225
if app.env is None or app.builder is None or app.builder.format != "html":
212226
return
213-
js_files = NbMetadataCollector.get_js_files(app.env, pagename) # type: ignore
227+
js_files = NbMetadataCollector.get_js_files(app.env, pagename)
214228
for path, kwargs in js_files.values():
215-
app.add_js_file(path, **kwargs) # type: ignore
229+
app.add_js_file(path, **kwargs)

0 commit comments

Comments
 (0)