Skip to content

Addition of black-nb to CI check to avoid inconsistency in indentation #4020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions check/nbformat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
################################################################################

only_print=1
for arg in $@; do
for arg in "$@"; do
if [[ "${arg}" == "--apply" ]]; then
only_print=0
else
Expand All @@ -25,29 +25,33 @@ for arg in $@; do
done

# Get the working directory to the repo root.
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$(git rev-parse --show-toplevel)"
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" || exit
cd "$(git rev-parse --show-toplevel)" || exit

# Install a pinned version of tf-docs
TF_DOCS_COMMIT=b3dc8a922d8bdc6e998a8ad4f4953359dc6e576a
TF_DOCS_INSTALLED=$(pip list | grep -c $TF_DOCS_COMMIT)
TF_DOCS_INSTALLED=$(pip list | grep -c "$TF_DOCS_COMMIT")

if [ "$TF_DOCS_INSTALLED" != "1" ]; then
pip install -U git+https://github.com/tensorflow/docs@$TF_DOCS_COMMIT
fi

FORMAT_CMD="python3 -m tensorflow_docs.tools.nbfmt --indent=1"
TFDOCS_FORMAT="black-nb"

# Test the notebooks
unformatted=$($FORMAT_CMD --test docs 2>&1 | grep "\- docs" || true)
black_unformatted="$("$TFDOCS_FORMAT" --check docs 2>&1 | grep "would be reformatted")"
unformatted="$("$FORMAT_CMD" --test docs 2>&1 | grep "\- docs" || true)"
needed_changes=0
if [ ! -z "${unformatted}" ]; then
if [ -n "${unformatted}" ] || [ -n "${black_unformatted}" ]; then
needed_changes=1
if (( only_print == 0 )); then
$FORMAT_CMD docs
"$TFDOCS_FORMAT" docs
"$FORMAT_CMD" docs
else
echo -e "\033[31mThe following notebooks require formatting\033[0m."
echo "${unformatted}"
echo "${black_unformatted}"
fi
fi

Expand All @@ -58,4 +62,4 @@ elif (( only_print == 1 )); then
exit 1
else
echo -e "\033[33mReformatted changed notebooks\033[0m."
fi
fi
3 changes: 2 additions & 1 deletion dev_tools/conf/pip-list-dev-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
asv
virtualenv
black==20.8b1
black-nb~=0.4.0
mypy~=0.782.0
pylint~=2.6.0
pytest~=6.2.2
Expand Down Expand Up @@ -39,4 +40,4 @@ rstcheck~=3.3.1
freezegun~=0.3.15

# for python 3.7 and below needs to be installed
importlib-metadata; python_version < '3.8'
importlib-metadata; python_version < '3.8'