From 6b8c32c6383abf994e26d505bcd4a0eb0dcf2428 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 8 Nov 2023 06:59:15 -0600 Subject: [PATCH] Use ruff on notebooks and update typings (#2068) --- .pre-commit-config.yaml | 4 +- CHANGELOG.md | 1 + docs/source/nbconvert_library.ipynb | 37 ++--- nbconvert/preprocessors/execute.py | 2 +- pyproject.toml | 10 +- tests/exporters/files/notebook_inject.ipynb | 88 ++++------ tests/files/Unexecuted_widget.ipynb | 6 +- tests/files/Unexecuted_widget_2.ipynb | 6 +- tests/files/Widget_List.ipynb | 150 +++++++++--------- tests/files/markdown_display_priority.ipynb | 2 +- tests/files/notebook1.ipynb | 8 +- tests/files/notebook2.ipynb | 6 +- tests/files/notebook3_with_errors.ipynb | 4 +- tests/files/notebook4_jpeg.ipynb | 2 +- tests/preprocessors/files/MixedMarkdown.ipynb | 2 +- tests/test_nbconvertapp.py | 6 +- 16 files changed, 165 insertions(+), 169 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9a08d6e47..801b7973f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -65,7 +65,7 @@ repos: "nbformat", "markupsafe", "mistune", - "nbclient", + "nbclient>=0.9", "defusedxml", "ipython", "packaging", @@ -84,8 +84,10 @@ repos: rev: v0.1.4 hooks: - id: ruff + types_or: [python, jupyter] args: ["--fix", "--show-fixes"] - id: ruff-format + types_or: [python, jupyter] - repo: https://github.com/scientific-python/cookie rev: "2023.10.27" diff --git a/CHANGELOG.md b/CHANGELOG.md index f47eaafe4..a9e881b84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1374,6 +1374,7 @@ raw template {%- endblock in_prompt -%} """ + exporter_attr = AttrExporter() output_attr, _ = exporter_attr.from_notebook_node(nb) assert "raw template" in output_attr diff --git a/docs/source/nbconvert_library.ipynb b/docs/source/nbconvert_library.ipynb index cb3f8e37b..102275758 100644 --- a/docs/source/nbconvert_library.ipynb +++ b/docs/source/nbconvert_library.ipynb @@ -46,9 +46,9 @@ "source": [ "from urllib.request import urlopen\n", "\n", - "url = 'https://jakevdp.github.io/downloads/notebooks/XKCD_plots.ipynb'\n", + "url = \"https://jakevdp.github.io/downloads/notebooks/XKCD_plots.ipynb\"\n", "response = urlopen(url).read().decode()\n", - "response[0:60] + ' ...'" + "response[0:60] + \" ...\"" ] }, { @@ -102,7 +102,7 @@ "\n", "# 2. Instantiate the exporter. We use the `classic` template for now; we'll get into more details\n", "# later about how to customize the exporter further.\n", - "html_exporter = HTMLExporter(template_name='classic')\n", + "html_exporter = HTMLExporter(template_name=\"classic\")\n", "\n", "# 3. Process the notebook we loaded earlier\n", "(body, resources) = html_exporter.from_notebook_node(jake_notebook)" @@ -125,7 +125,7 @@ }, "outputs": [], "source": [ - "print(body[:400] + '...')" + "print(body[:400] + \"...\")" ] }, { @@ -148,9 +148,9 @@ "outputs": [], "source": [ "print(\"Resources:\", resources.keys())\n", - "print(\"Metadata:\", resources['metadata'].keys())\n", - "print(\"Inlining:\", resources['inlining'].keys())\n", - "print(\"Extension:\", resources['output_extension'])" + "print(\"Metadata:\", resources[\"metadata\"].keys())\n", + "print(\"Inlining:\", resources[\"inlining\"].keys())\n", + "print(\"Extension:\", resources[\"output_extension\"])" ] }, { @@ -192,9 +192,9 @@ "# Convert the notebook to RST format\n", "(body, resources) = rst_exporter.from_notebook_node(jake_notebook)\n", "\n", - "print(body[:970] + '...')\n", - "print('[.....]')\n", - "print(body[800:1200] + '...')" + "print(body[:970] + \"...\")\n", + "print(\"[.....]\")\n", + "print(body[800:1200] + \"...\")" ] }, { @@ -218,7 +218,7 @@ }, "outputs": [], "source": [ - "sorted(resources['outputs'].keys())" + "sorted(resources[\"outputs\"].keys())" ] }, { @@ -240,7 +240,7 @@ "source": [ "from IPython.display import Image\n", "\n", - "Image(data=resources['outputs']['output_3_0.png'], format='png')" + "Image(data=resources[\"outputs\"][\"output_3_0.png\"], format=\"png\")" ] }, { @@ -302,7 +302,7 @@ "from traitlets.config import Config\n", "\n", "c = Config()\n", - "c.HTMLExporter.preprocessors = ['nbconvert.preprocessors.ExtractOutputPreprocessor']\n", + "c.HTMLExporter.preprocessors = [\"nbconvert.preprocessors.ExtractOutputPreprocessor\"]\n", "\n", "# create the new exporter using the custom config\n", "html_exporter_with_figs = HTMLExporter(config=c)\n", @@ -336,7 +336,7 @@ "print(sorted(resources_with_fig.keys()))\n", "\n", "print(\"\\nthe actual figures are:\")\n", - "print(sorted(resources_with_fig['outputs'].keys()))" + "print(sorted(resources_with_fig[\"outputs\"].keys()))" ] }, { @@ -382,6 +382,7 @@ "outputs": [], "source": [ "from traitlets import Integer\n", + "\n", "from nbconvert.preprocessors import Preprocessor\n", "\n", "\n", @@ -450,8 +451,8 @@ "\n", "dl = DictLoader(\n", " {\n", - " 'footer': \"\"\"\n", - "{%- extends 'lab/index.html.j2' -%} \n", + " \"footer\": \"\"\"\n", + "{%- extends 'lab/index.html.j2' -%}\n", "\n", "{% block footer %}\n", "FOOOOOOOOTEEEEER\n", @@ -461,9 +462,9 @@ ")\n", "\n", "\n", - "exportHTML = HTMLExporter(extra_loaders=[dl], template_file='footer')\n", + "exportHTML = HTMLExporter(extra_loaders=[dl], template_file=\"footer\")\n", "(body, resources) = exportHTML.from_notebook_node(jake_notebook)\n", - "for l in body.split('\\n')[-4:]:\n", + "for l in body.split(\"\\n\")[-4:]:\n", " print(l)" ] }, diff --git a/nbconvert/preprocessors/execute.py b/nbconvert/preprocessors/execute.py index 57728e056..ab2b8c2cf 100644 --- a/nbconvert/preprocessors/execute.py +++ b/nbconvert/preprocessors/execute.py @@ -34,7 +34,7 @@ def executenb(*args, **kwargs): # We inherit from both classes to allow for traitlets to resolve as they did pre-6.0. # This unfortunately makes for some ugliness around initialization as NotebookClient # assumes it's a constructed class with a nb object that we have to hack around. -class ExecutePreprocessor(Preprocessor, NotebookClient): # type:ignore[misc] +class ExecutePreprocessor(Preprocessor, NotebookClient): """ Executes all the cells in a notebook """ diff --git a/pyproject.toml b/pyproject.toml index 718205bf4..00070f41e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -115,7 +115,10 @@ nowarn = "test -W default {args}" detached = true dependencies = ["pre-commit"] [tool.hatch.envs.lint.scripts] -build = "pre-commit run --all-files ruff" +build = [ + "pre-commit run --all-files ruff", + "pre-commit run --all-files ruff-format", +] [tool.hatch.envs.typing] dependencies = [ "pre-commit"] @@ -224,6 +227,11 @@ unfixable = [ # TID252 Relative imports from parent modules are banned # PLR2004 Magic value used in comparison "tests/*" = ["B011", "F841", "S101", "TID252", "PLR2004"] +"tests/*/*.ipynb" = [ + "EM", "B018", "E402", "F405", "SIM", "F403", "F821", + "RUF100", "ICN001", "S605", "S607" +] +"docs/*/*.ipynb" = ["S310", "EM", "B018", "E402", "F405", "SIM", "F811", "N816","E741"] # F401 'foo' imported but unused # F403 'x' used; unable to detect undefined names "nbconvert/*/__init__.py" = ["F401", "F403"] diff --git a/tests/exporters/files/notebook_inject.ipynb b/tests/exporters/files/notebook_inject.ipynb index f7f7173e4..d60b701bd 100644 --- a/tests/exporters/files/notebook_inject.ipynb +++ b/tests/exporters/files/notebook_inject.ipynb @@ -35,19 +35,17 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/svg+xml": [ "" ] }, "execution_count": null, - "metadata": {} + "metadata": {}, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -56,7 +54,6 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/png": [ "" @@ -67,12 +64,11 @@ "filenames": { "image/png": "\">" } - } + }, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -81,7 +77,6 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/jpeg": [ "" @@ -92,12 +87,11 @@ "filenames": { "image/jpeg": "\">" } - } + }, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -106,19 +100,17 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/png": [ "\">" ] }, "execution_count": null, - "metadata": {} + "metadata": {}, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -127,19 +119,17 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/jpeg": [ "\">" ] }, "execution_count": null, - "metadata": {} + "metadata": {}, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -148,7 +138,6 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/png": [ "abcd" @@ -157,12 +146,11 @@ "execution_count": null, "metadata": { "width": ">" - } + }, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -171,7 +159,6 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/png": [ "abcd" @@ -182,12 +169,11 @@ "width": [ ">" ] - } + }, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -196,7 +182,6 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "image/png": [ "abcd" @@ -205,12 +190,11 @@ "execution_count": null, "metadata": { "height": ">" - } + }, + "output_type": "execute_result" } ], - "source": [ - "" - ] + "source": [] }, { "cell_type": "code", @@ -219,20 +203,18 @@ "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "wid1", - "foo": "\"", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", @@ -323,12 +304,13 @@ "pygments_lexer": "ipython3", "version": "3.10.5" }, + "title": "TITLE", "widgets": { "application/vnd.jupyter.widget-state+json": { + "foo": "pwntester