Skip to content

Commit 6b8c32c

Browse files
authored
Use ruff on notebooks and update typings (#2068)
1 parent d10418d commit 6b8c32c

16 files changed

+165
-169
lines changed

.pre-commit-config.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ repos:
6565
"nbformat",
6666
"markupsafe",
6767
"mistune",
68-
"nbclient",
68+
"nbclient>=0.9",
6969
"defusedxml",
7070
"ipython",
7171
"packaging",
@@ -84,8 +84,10 @@ repos:
8484
rev: v0.1.4
8585
hooks:
8686
- id: ruff
87+
types_or: [python, jupyter]
8788
args: ["--fix", "--show-fixes"]
8889
- id: ruff-format
90+
types_or: [python, jupyter]
8991

9092
- repo: https://github.com/scientific-python/cookie
9193
rev: "2023.10.27"

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,7 @@ raw template
13741374
{%- endblock in_prompt -%}
13751375
"""
13761376

1377+
13771378
exporter_attr = AttrExporter()
13781379
output_attr, _ = exporter_attr.from_notebook_node(nb)
13791380
assert "raw template" in output_attr

docs/source/nbconvert_library.ipynb

+19-18
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
"source": [
4747
"from urllib.request import urlopen\n",
4848
"\n",
49-
"url = 'https://jakevdp.github.io/downloads/notebooks/XKCD_plots.ipynb'\n",
49+
"url = \"https://jakevdp.github.io/downloads/notebooks/XKCD_plots.ipynb\"\n",
5050
"response = urlopen(url).read().decode()\n",
51-
"response[0:60] + ' ...'"
51+
"response[0:60] + \" ...\""
5252
]
5353
},
5454
{
@@ -102,7 +102,7 @@
102102
"\n",
103103
"# 2. Instantiate the exporter. We use the `classic` template for now; we'll get into more details\n",
104104
"# later about how to customize the exporter further.\n",
105-
"html_exporter = HTMLExporter(template_name='classic')\n",
105+
"html_exporter = HTMLExporter(template_name=\"classic\")\n",
106106
"\n",
107107
"# 3. Process the notebook we loaded earlier\n",
108108
"(body, resources) = html_exporter.from_notebook_node(jake_notebook)"
@@ -125,7 +125,7 @@
125125
},
126126
"outputs": [],
127127
"source": [
128-
"print(body[:400] + '...')"
128+
"print(body[:400] + \"...\")"
129129
]
130130
},
131131
{
@@ -148,9 +148,9 @@
148148
"outputs": [],
149149
"source": [
150150
"print(\"Resources:\", resources.keys())\n",
151-
"print(\"Metadata:\", resources['metadata'].keys())\n",
152-
"print(\"Inlining:\", resources['inlining'].keys())\n",
153-
"print(\"Extension:\", resources['output_extension'])"
151+
"print(\"Metadata:\", resources[\"metadata\"].keys())\n",
152+
"print(\"Inlining:\", resources[\"inlining\"].keys())\n",
153+
"print(\"Extension:\", resources[\"output_extension\"])"
154154
]
155155
},
156156
{
@@ -192,9 +192,9 @@
192192
"# Convert the notebook to RST format\n",
193193
"(body, resources) = rst_exporter.from_notebook_node(jake_notebook)\n",
194194
"\n",
195-
"print(body[:970] + '...')\n",
196-
"print('[.....]')\n",
197-
"print(body[800:1200] + '...')"
195+
"print(body[:970] + \"...\")\n",
196+
"print(\"[.....]\")\n",
197+
"print(body[800:1200] + \"...\")"
198198
]
199199
},
200200
{
@@ -218,7 +218,7 @@
218218
},
219219
"outputs": [],
220220
"source": [
221-
"sorted(resources['outputs'].keys())"
221+
"sorted(resources[\"outputs\"].keys())"
222222
]
223223
},
224224
{
@@ -240,7 +240,7 @@
240240
"source": [
241241
"from IPython.display import Image\n",
242242
"\n",
243-
"Image(data=resources['outputs']['output_3_0.png'], format='png')"
243+
"Image(data=resources[\"outputs\"][\"output_3_0.png\"], format=\"png\")"
244244
]
245245
},
246246
{
@@ -302,7 +302,7 @@
302302
"from traitlets.config import Config\n",
303303
"\n",
304304
"c = Config()\n",
305-
"c.HTMLExporter.preprocessors = ['nbconvert.preprocessors.ExtractOutputPreprocessor']\n",
305+
"c.HTMLExporter.preprocessors = [\"nbconvert.preprocessors.ExtractOutputPreprocessor\"]\n",
306306
"\n",
307307
"# create the new exporter using the custom config\n",
308308
"html_exporter_with_figs = HTMLExporter(config=c)\n",
@@ -336,7 +336,7 @@
336336
"print(sorted(resources_with_fig.keys()))\n",
337337
"\n",
338338
"print(\"\\nthe actual figures are:\")\n",
339-
"print(sorted(resources_with_fig['outputs'].keys()))"
339+
"print(sorted(resources_with_fig[\"outputs\"].keys()))"
340340
]
341341
},
342342
{
@@ -382,6 +382,7 @@
382382
"outputs": [],
383383
"source": [
384384
"from traitlets import Integer\n",
385+
"\n",
385386
"from nbconvert.preprocessors import Preprocessor\n",
386387
"\n",
387388
"\n",
@@ -450,8 +451,8 @@
450451
"\n",
451452
"dl = DictLoader(\n",
452453
" {\n",
453-
" 'footer': \"\"\"\n",
454-
"{%- extends 'lab/index.html.j2' -%} \n",
454+
" \"footer\": \"\"\"\n",
455+
"{%- extends 'lab/index.html.j2' -%}\n",
455456
"\n",
456457
"{% block footer %}\n",
457458
"FOOOOOOOOTEEEEER\n",
@@ -461,9 +462,9 @@
461462
")\n",
462463
"\n",
463464
"\n",
464-
"exportHTML = HTMLExporter(extra_loaders=[dl], template_file='footer')\n",
465+
"exportHTML = HTMLExporter(extra_loaders=[dl], template_file=\"footer\")\n",
465466
"(body, resources) = exportHTML.from_notebook_node(jake_notebook)\n",
466-
"for l in body.split('\\n')[-4:]:\n",
467+
"for l in body.split(\"\\n\")[-4:]:\n",
467468
" print(l)"
468469
]
469470
},

nbconvert/preprocessors/execute.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def executenb(*args, **kwargs):
3434
# We inherit from both classes to allow for traitlets to resolve as they did pre-6.0.
3535
# This unfortunately makes for some ugliness around initialization as NotebookClient
3636
# assumes it's a constructed class with a nb object that we have to hack around.
37-
class ExecutePreprocessor(Preprocessor, NotebookClient): # type:ignore[misc]
37+
class ExecutePreprocessor(Preprocessor, NotebookClient):
3838
"""
3939
Executes all the cells in a notebook
4040
"""

pyproject.toml

+9-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ nowarn = "test -W default {args}"
115115
detached = true
116116
dependencies = ["pre-commit"]
117117
[tool.hatch.envs.lint.scripts]
118-
build = "pre-commit run --all-files ruff"
118+
build = [
119+
"pre-commit run --all-files ruff",
120+
"pre-commit run --all-files ruff-format",
121+
]
119122

120123
[tool.hatch.envs.typing]
121124
dependencies = [ "pre-commit"]
@@ -224,6 +227,11 @@ unfixable = [
224227
# TID252 Relative imports from parent modules are banned
225228
# PLR2004 Magic value used in comparison
226229
"tests/*" = ["B011", "F841", "S101", "TID252", "PLR2004"]
230+
"tests/*/*.ipynb" = [
231+
"EM", "B018", "E402", "F405", "SIM", "F403", "F821",
232+
"RUF100", "ICN001", "S605", "S607"
233+
]
234+
"docs/*/*.ipynb" = ["S310", "EM", "B018", "E402", "F405", "SIM", "F811", "N816","E741"]
227235
# F401 'foo' imported but unused
228236
# F403 'x' used; unable to detect undefined names
229237
"nbconvert/*/__init__.py" = ["F401", "F403"]

0 commit comments

Comments
 (0)