Skip to content

Commit b52511c

Browse files
committed
fix: use tensorflow_docs.tools.nbfmt for notebook checks (#1274)
The checker called a non-existent tensorflow_docs.tools.nbformat module. Align with Cirq by using nbfmt, regenerate lockfiles on current main, and format all tracked notebooks so CI can pass.
1 parent 8c043e4 commit b52511c

17 files changed

Lines changed: 730 additions & 544 deletions

check/nbformat

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,28 @@
1616
set -euo pipefail
1717

1818
declare -r usage="\
19-
Usage: ${0##*/} [--help] [--fix]
19+
Usage: ${0##*/} [--help] [--apply]
2020
21-
Checks the format of Jupyter notebook (.ipynb) files in this repository using
22-
the TensorFlow Docs notebook format checker (tensorflow_docs.tools.nbformat).
21+
Checks Jupyter notebook (.ipynb) format using tensorflow_docs.tools.nbfmt.
2322
24-
With no arguments, notebooks are checked but not modified. Exits with a
25-
non-zero status code if any notebook fails the format check.
26-
27-
With --fix, the notebooks are reformatted in place and the script exits with
28-
status 0 if all notebooks were successfully processed.
23+
Without --apply, exits 0 if all notebooks are formatted and 1 otherwise.
24+
With --apply, reformats notebooks in place.
2925
"
3026

3127
# Get the working directory to the repo root.
3228
thisdir=$(dirname "${BASH_SOURCE[0]:?}")
3329
repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel)
3430
cd "${repo_dir}"
3531

36-
# Parse arguments.
37-
opt_fix=0
32+
opt_apply=0
3833
for arg in "$@"; do
3934
case "${arg}" in
4035
-h | --help)
4136
echo "${usage}"
4237
exit 0
4338
;;
44-
--fix)
45-
opt_fix=1
39+
--apply)
40+
opt_apply=1
4641
;;
4742
*)
4843
echo "Unknown argument: '${arg}'" >&2
@@ -52,47 +47,32 @@ for arg in "$@"; do
5247
esac
5348
done
5449

55-
# Find all tracked .ipynb files in the repository.
56-
IFS=$'\n' read -r -d '' -a notebooks < <(
57-
git ls-files '*.ipynb'
58-
printf "\0"
59-
)
60-
61-
if [[ "${#notebooks[@]}" -eq 0 ]]; then
62-
echo "No .ipynb notebook files found."
63-
exit 0
64-
fi
65-
66-
echo "Found ${#notebooks[@]} notebook(s)."
67-
68-
# Build the tensorflow_docs.tools.nbformat argument list.
69-
declare -a tf_args=()
70-
if (( opt_fix )); then
71-
echo "Running notebook format fixer..."
72-
tf_args=( "--fix" )
73-
else
74-
echo "Running notebook format checker..."
50+
if ! python -c "import tensorflow_docs.tools.nbfmt"; then
51+
echo "tensorflow-docs is not installed." >&2
52+
exit 2
7553
fi
7654

77-
declare -a errors=()
78-
for notebook in "${notebooks[@]}"; do
79-
echo " Checking: ${notebook}"
80-
if ! python -m tensorflow_docs.tools.nbformat "${tf_args[@]}" "${notebook}"; then
81-
errors+=( "${notebook}" )
82-
fi
83-
done
55+
declare -a notebook_paths=(
56+
docs
57+
src/openfermion/resource_estimates/pbc/notebooks
58+
)
8459

85-
echo
60+
my_nbfmt() {
61+
python -m tensorflow_docs.tools.nbfmt --indent=1 "$@"
62+
}
8663

87-
if [[ "${#errors[@]}" -eq 0 ]]; then
88-
echo "All notebooks passed the format check."
89-
exit 0
64+
exit_code=0
65+
if output=$(my_nbfmt --test "${notebook_paths[@]}" 2>&1); then
66+
echo "Notebooks are formatted."
67+
elif (( opt_apply )); then
68+
my_nbfmt "${notebook_paths[@]}" &&
69+
echo "Reformatted notebooks." ||
70+
exit_code=$?
9071
else
91-
echo "The following notebooks failed the format check:" >&2
92-
printf " %s\n" "${errors[@]}" >&2
93-
if (( ! opt_fix )); then
94-
echo "" >&2
95-
echo "Run 'check/nbformat --fix' to fix the notebooks automatically." >&2
96-
fi
97-
exit 1
72+
echo "The following notebooks require formatting:" >&2
73+
grep '^-' <<<"${output}" >&2 || echo "${output}" >&2
74+
echo "Run 'check/nbformat --apply' to fix notebooks." >&2
75+
exit_code=1
9876
fi
77+
78+
exit "${exit_code}"

dev_tools/requirements/envs/dev.env.txt

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.10
2+
# This file is autogenerated by pip-compile with Python 3.13
33
# by the following command:
44
#
55
# pip-compile --output-file=envs/dev.env.txt deps/format.txt deps/mypy.txt deps/pip-tools.txt deps/pylint.txt deps/pytest.txt deps/resource_estimates_runtime.txt deps/runtime.txt deps/shellcheck.txt
66
#
7+
absl-py==2.4.0
8+
# via tensorflow-docs
79
ase==3.28.0
810
# via -r deps/resource_estimates_runtime.txt
911
ast-serialize==0.5.0
1012
# via mypy
13+
astor==0.8.1
14+
# via tensorflow-docs
1115
astroid==3.3.11
1216
# via pylint
1317
attrs==26.1.0
1418
# via
1519
# cirq-core
1620
# jsonschema
1721
# referencing
18-
backports-asyncio-runner==1.2.0 ; python_full_version < "3.11"
19-
# via
20-
# -r deps/runtime.txt
21-
# pytest-asyncio
2222
black==26.5.1
2323
# via -r deps/format.txt
2424
build==1.5.0
@@ -33,6 +33,13 @@ click==8.4.1
3333
# via
3434
# black
3535
# pip-tools
36+
colorama==0.4.6
37+
# via
38+
# build
39+
# click
40+
# pylint
41+
# pytest
42+
# tqdm
3643
contourpy==1.3.2
3744
# via
3845
# -r deps/runtime.txt
@@ -47,8 +54,6 @@ dill==0.4.1
4754
# via pylint
4855
duet==0.2.9
4956
# via cirq-core
50-
exceptiongroup==1.3.1
51-
# via pytest
5257
execnet==2.1.2
5358
# via pytest-xdist
5459
fastjsonschema==2.21.2
@@ -71,6 +76,8 @@ jaxlib==0.4.38
7176
# via
7277
# -r deps/resource_estimates_runtime.txt
7378
# jax
79+
jinja2==3.1.6
80+
# via tensorflow-docs
7481
jsonschema==4.26.0
7582
# via nbformat
7683
jsonschema-specifications==2025.9.1
@@ -81,6 +88,8 @@ kiwisolver==1.5.0
8188
# via matplotlib
8289
librt==0.11.0
8390
# via mypy
91+
markupsafe==3.0.3
92+
# via jinja2
8493
matplotlib==3.10.9
8594
# via
8695
# ase
@@ -100,7 +109,9 @@ mypy-extensions==1.1.0
100109
# black
101110
# mypy
102111
nbformat==5.10.4
103-
# via -r deps/pytest.txt
112+
# via
113+
# -r deps/pytest.txt
114+
# tensorflow-docs
104115
networkx==3.4.2
105116
# via
106117
# -r deps/runtime.txt
@@ -152,6 +163,8 @@ pluggy==1.6.0
152163
# via
153164
# pytest
154165
# pytest-cov
166+
protobuf==7.35.0
167+
# via tensorflow-docs
155168
pubchempy==1.0.5
156169
# via -r deps/runtime.txt
157170
pygments==2.20.0
@@ -192,6 +205,8 @@ pytokens==0.4.1
192205
# via black
193206
pytz==2026.2
194207
# via pandas
208+
pyyaml==6.0.3
209+
# via tensorflow-docs
195210
referencing==0.37.0
196211
# via
197212
# jsonschema
@@ -220,15 +235,8 @@ sympy==1.14.0
220235
# via
221236
# -r deps/runtime.txt
222237
# cirq-core
223-
tomli==2.4.1
224-
# via
225-
# black
226-
# build
227-
# coverage
228-
# mypy
229-
# pip-tools
230-
# pylint
231-
# pytest
238+
tensorflow-docs==2025.2.19.33219
239+
# via -r deps/pytest.txt
232240
tomlkit==0.15.0
233241
# via pylint
234242
tqdm==4.67.3
@@ -247,13 +255,8 @@ types-setuptools==82.0.0.20260518
247255
# via -r deps/mypy.txt
248256
typing-extensions==4.15.0
249257
# via
250-
# astroid
251-
# black
252258
# cirq-core
253-
# exceptiongroup
254259
# mypy
255-
# pytest-asyncio
256-
# referencing
257260
tzdata==2026.2
258261
# via pandas
259262
urllib3==2.7.0

dev_tools/requirements/envs/pytest.env.txt

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.10
2+
# This file is autogenerated by pip-compile with Python 3.13
33
# by the following command:
44
#
55
# pip-compile --constraint=envs/dev.env.txt --output-file=envs/pytest.env.txt deps/pytest.txt deps/runtime.txt
66
#
7+
absl-py==2.4.0
8+
# via
9+
# -c envs/dev.env.txt
10+
# tensorflow-docs
11+
astor==0.8.1
12+
# via
13+
# -c envs/dev.env.txt
14+
# tensorflow-docs
715
attrs==26.1.0
816
# via
917
# -c envs/dev.env.txt
1018
# cirq-core
1119
# jsonschema
1220
# referencing
13-
backports-asyncio-runner==1.2.0 ; python_full_version < "3.11"
14-
# via
15-
# -c envs/dev.env.txt
16-
# -r deps/runtime.txt
17-
# pytest-asyncio
1821
certifi==2026.5.20
1922
# via
2023
# -c envs/dev.env.txt
@@ -27,6 +30,11 @@ cirq-core==1.5.0
2730
# via
2831
# -c envs/dev.env.txt
2932
# -r deps/runtime.txt
33+
colorama==0.4.6
34+
# via
35+
# -c envs/dev.env.txt
36+
# pytest
37+
# tqdm
3038
contourpy==1.3.2
3139
# via
3240
# -c envs/dev.env.txt
@@ -48,10 +56,6 @@ duet==0.2.9
4856
# via
4957
# -c envs/dev.env.txt
5058
# cirq-core
51-
exceptiongroup==1.3.1
52-
# via
53-
# -c envs/dev.env.txt
54-
# pytest
5559
execnet==2.1.2
5660
# via
5761
# -c envs/dev.env.txt
@@ -76,6 +80,10 @@ iniconfig==2.3.0
7680
# via
7781
# -c envs/dev.env.txt
7882
# pytest
83+
jinja2==3.1.6
84+
# via
85+
# -c envs/dev.env.txt
86+
# tensorflow-docs
7987
jsonschema==4.26.0
8088
# via
8189
# -c envs/dev.env.txt
@@ -92,6 +100,10 @@ kiwisolver==1.5.0
92100
# via
93101
# -c envs/dev.env.txt
94102
# matplotlib
103+
markupsafe==3.0.3
104+
# via
105+
# -c envs/dev.env.txt
106+
# jinja2
95107
matplotlib==3.10.9
96108
# via
97109
# -c envs/dev.env.txt
@@ -104,6 +116,7 @@ nbformat==5.10.4
104116
# via
105117
# -c envs/dev.env.txt
106118
# -r deps/pytest.txt
119+
# tensorflow-docs
107120
networkx==3.4.2
108121
# via
109122
# -c envs/dev.env.txt
@@ -142,6 +155,10 @@ pluggy==1.6.0
142155
# -c envs/dev.env.txt
143156
# pytest
144157
# pytest-cov
158+
protobuf==7.35.0
159+
# via
160+
# -c envs/dev.env.txt
161+
# tensorflow-docs
145162
pubchempy==1.0.5
146163
# via
147164
# -c envs/dev.env.txt
@@ -192,6 +209,10 @@ pytz==2026.2
192209
# via
193210
# -c envs/dev.env.txt
194211
# pandas
212+
pyyaml==6.0.3
213+
# via
214+
# -c envs/dev.env.txt
215+
# tensorflow-docs
195216
referencing==0.37.0
196217
# via
197218
# -c envs/dev.env.txt
@@ -224,11 +245,10 @@ sympy==1.14.0
224245
# -c envs/dev.env.txt
225246
# -r deps/runtime.txt
226247
# cirq-core
227-
tomli==2.4.1
248+
tensorflow-docs==2025.2.19.33219
228249
# via
229250
# -c envs/dev.env.txt
230-
# coverage
231-
# pytest
251+
# -r deps/pytest.txt
232252
tqdm==4.67.3
233253
# via
234254
# -c envs/dev.env.txt
@@ -242,9 +262,6 @@ typing-extensions==4.15.0
242262
# via
243263
# -c envs/dev.env.txt
244264
# cirq-core
245-
# exceptiongroup
246-
# pytest-asyncio
247-
# referencing
248265
tzdata==2026.2
249266
# via
250267
# -c envs/dev.env.txt

0 commit comments

Comments
 (0)