From 512b0f905582e9fc02555bef4c240383eb56dbc5 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 13 Oct 2023 10:55:40 +0100 Subject: [PATCH 1/5] chore: bump pyupgrade version --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bb252abe..ce6bdcff 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: rev: v3.15.0 hooks: - id: pyupgrade - args: [--py37-plus] + args: [--py38-plus] - repo: https://github.com/PyCQA/isort rev: 5.12.0 From 0d0186ae61177bfbacbba5f30eda31f6d47b955d Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 13 Oct 2023 11:14:44 +0100 Subject: [PATCH 2/5] chore: update hooks & pyupgrade & ruff --- .pre-commit-config.yaml | 13 +++++++++---- myst_nb/core/config.py | 8 +------- myst_nb/core/render.py | 2 +- pyproject.toml | 6 ++++++ 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ce6bdcff..9892fdbc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,6 +18,11 @@ 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 @@ -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 diff --git a/myst_nb/core/config.py b/myst_nb/core/config.py index 3bf97e83..d3b5ad9d 100644 --- a/myst_nb/core/config.py +++ b/myst_nb/core/config.py @@ -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, @@ -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 diff --git a/myst_nb/core/render.py b/myst_nb/core/render.py index 443f6408..4e2a0ba3 100644 --- a/myst_nb/core/render.py +++ b/myst_nb/core/render.py @@ -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"): diff --git a/pyproject.toml b/pyproject.toml index edba5e63..3bd822c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 From 615a53107102fd540848e1ae1e3fd74554f96096 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 13 Oct 2023 11:17:12 +0100 Subject: [PATCH 3/5] chore: drop flake8 --- tox.ini | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tox.ini b/tox.ini index 3bfcea70..6ae14429 100644 --- a/tox.ini +++ b/tox.ini @@ -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 From 4342ce75fc67e5b9dd3e54ca7d47777d074dec71 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 13 Oct 2023 11:21:11 +0100 Subject: [PATCH 4/5] chore: run ruff/black on all files, update contributing --- docs/reference/contributing.md | 8 ++++---- myst_nb/sphinx_.py | 11 +++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/reference/contributing.md b/docs/reference/contributing.md index d0859122..7fec239a 100644 --- a/docs/reference/contributing.md +++ b/docs/reference/contributing.md @@ -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/) @@ -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. diff --git a/myst_nb/sphinx_.py b/myst_nb/sphinx_.py index f6c550f3..33d5f963 100644 --- a/myst_nb/sphinx_.py +++ b/myst_nb/sphinx_.py @@ -327,10 +327,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 From 3f0b89dd0fb164b2e1437ad15f9179e44b3752d3 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 13 Oct 2023 12:02:20 +0100 Subject: [PATCH 5/5] chore: appease linter --- myst_nb/docutils_.py | 2 +- myst_nb/sphinx_.py | 3 ++- myst_nb/sphinx_ext.py | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/myst_nb/docutils_.py b/myst_nb/docutils_.py index 7da9c6db..7731f0b5 100644 --- a/myst_nb/docutils_.py +++ b/myst_nb/docutils_.py @@ -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 diff --git a/myst_nb/sphinx_.py b/myst_nb/sphinx_.py index 33d5f963..4885d9ff 100644 --- a/myst_nb/sphinx_.py +++ b/myst_nb/sphinx_.py @@ -61,6 +61,8 @@ 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. @@ -68,7 +70,6 @@ def parse(self, inputstring: str, document: nodes.document) -> None: :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 diff --git a/myst_nb/sphinx_ext.py b/myst_nb/sphinx_ext.py index 1044e457..780fbce5 100644 --- a/myst_nb/sphinx_ext.py +++ b/myst_nb/sphinx_ext.py @@ -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 @@ -29,6 +29,7 @@ NbMetadataCollector, Parser, SelectMimeType, + SphinxEnvType, ) SPHINX_LOGGER = sphinx_logging.getLogger(__name__) @@ -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