diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bb252abe..9892fdbc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -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/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/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/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 f6c550f3..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 @@ -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 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 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 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