Skip to content
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
15 changes: 10 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ repos:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py37-plus]
args: [--py38-plus]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
Expand All @@ -35,11 +40,11 @@ repos:
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear]
- id: ruff
args: ["--fix", "--show-fixes"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
Expand Down
8 changes: 4 additions & 4 deletions docs/reference/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ that can be executed and read into Sphinx.

## Code Style

Code style is tested using [flake8](http://flake8.pycqa.org),
with the configuration set in `.flake8`,
Code style is tested using [ruff](https://docs.astral.sh/ruff),
with the configuration set in `pyproject.toml`,
and code formatted with [black](https://github.com/ambv/black).

Installing with `myst-nb[code_style]` makes the [pre-commit](https://pre-commit.com/)
Expand All @@ -52,11 +52,11 @@ It can be setup by:
>> pre-commit install
```

Optionally you can run `black` and `flake8` separately:
Optionally you can run `black` and `ruff` separately:

```shell
>> black .
>> flake8 .
>> ruff .
```

Editors like VS Code also have automatic code reformat utilities, which can adhere to this standard.
Expand Down
8 changes: 1 addition & 7 deletions myst_nb/core/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Configuration for myst-nb."""
import dataclasses as dc
from enum import Enum
import sys
from typing import Any, Callable, Dict, Iterable, Optional, Sequence, Tuple
from typing import Any, Callable, Dict, Iterable, Literal, Optional, Sequence, Tuple

from myst_parser.config.dc_validators import (
ValidatorType,
Expand All @@ -14,11 +13,6 @@
validate_fields,
)

if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal # noqa: F401

from myst_nb.warnings_ import MystNBWarnings


Expand Down
2 changes: 1 addition & 1 deletion myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def handle_mime(
return None


@lru_cache()
@lru_cache
def load_mime_renders() -> list[MimeRenderPlugin]:
all_eps = entry_points()
if hasattr(all_eps, "select"):
Expand Down
2 changes: 1 addition & 1 deletion myst_nb/docutils_.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,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
14 changes: 9 additions & 5 deletions myst_nb/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ class Parser(MystParser):
config_section = "myst-nb parser"
config_section_dependencies = ("parsers",)

env: SphinxEnvType

def parse(self, inputstring: str, document: nodes.document) -> None:
"""Parse source text.

:param inputstring: The source string to parse
:param document: The root docutils node to add AST elements to
"""
assert self.env is not None, "env not set"
self.env: SphinxEnvType
document_path = self.env.doc2path(self.env.docname)

# get a logger for this document
Expand Down Expand Up @@ -327,10 +328,13 @@ def run(self, **kwargs: Any) -> None:
priority_list = get_mime_priority(
bname, self.config["nb_mime_priority_overrides"]
)
condition = (
lambda node: isinstance(node, nodes.container)
and node.attributes.get("nb_element", "") == "mime_bundle"
)

def condition(node):
return (
isinstance(node, nodes.container)
and node.attributes.get("nb_element", "") == "mime_bundle"
)

# remove/replace_self will not work with an iterator
for node in list(findall(self.document)(condition)):
# get available mime types
Expand Down
7 changes: 4 additions & 3 deletions myst_nb/sphinx_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
import sys
from types import ModuleType
from typing import Any, Iterator
from typing import Any, Iterator, cast

from myst_parser.sphinx_ext.main import setup_sphinx as setup_myst_parser
from sphinx.application import Sphinx
Expand All @@ -29,6 +29,7 @@
NbMetadataCollector,
Parser,
SelectMimeType,
SphinxEnvType,
)

SPHINX_LOGGER = sphinx_logging.getLogger(__name__)
Expand Down Expand Up @@ -224,6 +225,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)
js_files = NbMetadataCollector.get_js_files(cast(SphinxEnvType, app.env), pagename)
for path, kwargs in js_files.values():
app.add_js_file(path, **kwargs)
app.add_js_file(path, **kwargs) # type: ignore
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,9 @@ ignore_missing_imports = true
profile = "black"
src_paths = ["myst_nb", "tests"]
force_sort_within_sections = true

[tool.ruff]
extend-ignore = [
"E203", # Whitespace before punctuation
]
line-length = 100
4 changes: 0 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,3 @@ commands_post = echo "open file://{toxinidir}/docs/_build/{env:BUILDER}/index.ht
[pytest]
markers =
sphinx_params: Specify parameters to pass to the sphinx_run fixture

[flake8]
max-line-length = 100
extend-ignore = E203