Skip to content

Commit 6be37cd

Browse files
authored
⬆️ UPGRADE: markdown-it-py v1.0 (#320)
Updated via dependencies which support this version; - myst-parser -> 0.14.0 - jupytext -> 1.11.2 See https://github.com/executablebooks/markdown-it-py/blob/master/CHANGELOG.md and https://github.com/executablebooks/MyST-Parser/blob/master/CHANGELOG.md Also added mypy type checking for the parser code.
1 parent 3c425d3 commit 6be37cd

8 files changed

Lines changed: 58 additions & 18 deletions

File tree

.pre-commit-config.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,20 @@ repos:
4242
- id: flake8
4343
additional_dependencies: [flake8-bugbear==21.3.1]
4444

45+
- repo: https://github.com/pre-commit/mirrors-mypy
46+
rev: v0.812
47+
hooks:
48+
- id: mypy
49+
args: [--config-file=setup.cfg]
50+
additional_dependencies:
51+
- myst-parser~=0.14.0
52+
files: >
53+
(?x)^(
54+
myst_nb/parser.py|
55+
)$
56+
4557
# this is not used for now,
46-
# since it converts myst-parser to myst_parser and removes comments
58+
# since it converts myst-nb to myst_nb and removes comments
4759
# - repo: https://github.com/asottile/setup-cfg-fmt
4860
# rev: v1.17.0
4961
# hooks:

docs/examples/custom-formats.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ nb_custom_formats = {
7171
```
7272

7373
:::{important}
74-
For full compatibility with `myst-nb`, `jupytext>=1.8.0` should be used.
74+
For full compatibility with `myst-nb`, `jupytext>=1.11.2` should be used.
7575
:::
7676

7777
For example:

myst_nb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.12.3"
1+
__version__ = "0.13.0"
22

33
import os
44
from collections.abc import Sequence

myst_nb/parser.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import List, Tuple
2+
from typing import Any, Dict, List, Tuple
33

44
import nbformat as nbf
55
from docutils import nodes
@@ -8,7 +8,7 @@
88
from markdown_it import MarkdownIt
99
from markdown_it.rules_core import StateCore
1010
from markdown_it.token import Token
11-
from markdown_it.utils import AttrDict
11+
from markdown_it.tree import SyntaxTreeNode
1212
from myst_parser.main import MdParserConfig, default_parser
1313
from myst_parser.sphinx_parser import MystParser
1414
from myst_parser.sphinx_renderer import SphinxRenderer
@@ -33,7 +33,9 @@ class NotebookParser(MystParser):
3333
config_section = "myst-nb parser"
3434
config_section_dependencies = ("parsers",)
3535

36-
def parse(self, inputstring: str, document: nodes.document):
36+
def parse(
37+
self, inputstring: str, document: nodes.document, renderer: str = "sphinx"
38+
) -> None:
3739

3840
self.reporter = document.reporter
3941
self.env = document.settings.env # type: BuildEnvironment
@@ -70,7 +72,11 @@ def parse(self, inputstring: str, document: nodes.document):
7072
# containing global data like reference definitions
7173
md_parser, env, tokens = nb_to_tokens(
7274
ntbk,
73-
self.env.myst_config if converter is None else converter.config,
75+
(
76+
self.env.myst_config # type: ignore[attr-defined]
77+
if converter is None
78+
else converter.config
79+
),
7480
self.env.config["nb_render_plugin"],
7581
)
7682

@@ -87,7 +93,7 @@ def parse(self, inputstring: str, document: nodes.document):
8793

8894
def nb_to_tokens(
8995
ntbk: nbf.NotebookNode, config: MdParserConfig, renderer_plugin: str
90-
) -> Tuple[MarkdownIt, AttrDict, List[Token]]:
96+
) -> Tuple[MarkdownIt, Dict[str, Any], List[Token]]:
9197
"""Parse the notebook content to a list of syntax tokens and an env,
9298
containing global data like reference definitions.
9399
"""
@@ -99,7 +105,7 @@ def nb_to_tokens(
99105
md.renderer = SphinxNBRenderer(md)
100106
# make a sandbox where all the parsing global data,
101107
# like reference definitions will be stored
102-
env = AttrDict()
108+
env: Dict[str, Any] = {}
103109
rules = md.core.ruler.get_active_rules()
104110

105111
# First only run pre-inline chains
@@ -185,7 +191,7 @@ def parse_block(src, start_line):
185191
"",
186192
0,
187193
map=[0, 0],
188-
content=({k: v for k, v in ntbk.metadata.items()}),
194+
content=({k: v for k, v in ntbk.metadata.items()}), # type: ignore[arg-type]
189195
)
190196
] + state.tokens
191197

@@ -205,8 +211,8 @@ def parse_block(src, start_line):
205211

206212

207213
def tokens_to_docutils(
208-
md: MarkdownIt, env: AttrDict, tokens: List[Token], document: nodes.document
209-
):
214+
md: MarkdownIt, env: Dict[str, Any], tokens: List[Token], document: nodes.document
215+
) -> None:
210216
"""Render the Markdown tokens to docutils AST."""
211217
md.options["document"] = document
212218
md.renderer.render(tokens, md.options, env)
@@ -217,14 +223,14 @@ class SphinxNBRenderer(SphinxRenderer):
217223
which includes special methods for notebook cells.
218224
"""
219225

220-
def render_jupyter_widget_state(self, token: Token):
226+
def render_jupyter_widget_state(self, token: SyntaxTreeNode) -> None:
221227
if token.meta["state"]:
222228
self.document.settings.env.nb_contains_widgets = True
223229
node = JupyterWidgetStateNode(state=token.meta["state"])
224230
self.add_line_and_source_path(node, token)
225231
self.document.append(node)
226232

227-
def render_nb_code_cell(self, token: Token):
233+
def render_nb_code_cell(self, token: SyntaxTreeNode) -> None:
228234
"""Render a Jupyter notebook cell."""
229235
cell = token.meta["cell"] # type: nbf.NotebookNode
230236

setup.cfg

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ keywords =
2929
markdown
3030
lexer
3131
parser
32-
development
32+
jupyter
3333
docutils
3434
sphinx
3535
project_urls =
@@ -44,7 +44,7 @@ install_requires =
4444
ipywidgets>=7.0.0,<8
4545
jupyter-cache~=0.4.1
4646
jupyter_sphinx~=0.3.2
47-
myst-parser~=0.13.5
47+
myst-parser~=0.14.0
4848
nbconvert~=5.6
4949
nbformat~=5.0
5050
pyyaml
@@ -74,7 +74,7 @@ rtd =
7474
bokeh
7575
coconut~=1.4.3
7676
ipywidgets
77-
jupytext~=1.8.0
77+
jupytext~=1.11.2
7878
matplotlib
7979
numpy
8080
pandas
@@ -86,7 +86,7 @@ rtd =
8686
sympy
8787
testing =
8888
coverage<5.0
89-
jupytext~=1.8.0
89+
jupytext~=1.11.2
9090
# TODO: 3.4.0 has some warnings that need to be fixed in the tests.
9191
matplotlib~=3.3.0
9292
numpy
@@ -99,3 +99,20 @@ testing =
9999
[flake8]
100100
max-line-length = 100
101101
extend-ignore = E203
102+
103+
[mypy]
104+
show_error_codes = true
105+
check_untyped_defs = true
106+
strict_equality = true
107+
no_implicit_optional = true
108+
warn_unused_ignores = true
109+
110+
[mypy-myst_nb.*]
111+
; can only follow these imports when more of the code is typed
112+
follow_imports = skip
113+
114+
[mypy-jupyter_sphinx.*]
115+
ignore_missing_imports = True
116+
117+
[mypy-nbformat.*]
118+
ignore_missing_imports = True

tests/test_mystnb_features.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_codecell_file(sphinx_run, file_regression, check_nbs, get_test_path):
1717
"author",
1818
"source_map",
1919
"language_info",
20+
"wordcount",
2021
}
2122
assert sphinx_run.app.env.metadata["mystnb_codecell_file"]["author"] == "Matt"
2223
assert (
@@ -50,6 +51,7 @@ def test_codecell_file_warnings(sphinx_run, file_regression, check_nbs, get_test
5051
"author",
5152
"source_map",
5253
"language_info",
54+
"wordcount",
5355
}
5456
assert (
5557
sphinx_run.app.env.metadata["mystnb_codecell_file_warnings"]["author"]

tests/test_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_basic_run(sphinx_run, file_regression):
1010
"test_name",
1111
"kernelspec",
1212
"language_info",
13+
"wordcount",
1314
}
1415
assert sphinx_run.app.env.metadata["basic_run"]["test_name"] == "notebook1"
1516
assert (
@@ -44,6 +45,7 @@ def test_complex_outputs(sphinx_run, file_regression):
4445
"jupytext",
4546
"toc",
4647
"varInspector",
48+
"wordcount",
4749
}
4850
assert (
4951
sphinx_run.app.env.metadata["complex_outputs"]["celltoolbar"] == "Edit Metadata"

tests/test_text_based.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_basic_run(sphinx_run, file_regression, check_nbs):
1515
"author",
1616
"source_map",
1717
"language_info",
18+
"wordcount",
1819
}
1920
assert sphinx_run.app.env.metadata["basic_unrun"]["author"] == "Chris"
2021
assert (

0 commit comments

Comments
 (0)