Skip to content

Update format-incremental script to format notebooks #7342

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

Merged
Merged
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
24 changes: 12 additions & 12 deletions check/format-incremental
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env bash

################################################################################
# Formats python files that have been modified.
# Formats python and notebook files that have been modified.
#
# Usage:
# check/format-incremental [BASE_REVISION] [--apply] [--all]
#
# By default, the script analyzes python files that have changed relative to the
# base revision and determines whether they need to be formatted. If any changes
# are needed, it prints the diff and exits with code 1, otherwise it exits with
# code 0.
# By default, the script analyzes python and notebook files that have changed
# relative to the base revision and determines whether they need to be
# formatted. If any changes are needed, it prints the diff and exits with
# code 1, otherwise it exits with code 0.
#
# With '--apply', reformats the files instead of printing the diff and exits
# with code 0.
#
# With '--all', analyzes all python files, instead of only changed files.
# With '--all', analyzes all files, instead of only changed files.
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a file is considered to have "changed"). For
Expand Down Expand Up @@ -80,25 +80,25 @@ if (( only_changed == 1 )); then
rev="${base}"
fi

# Get the modified, added and moved python files.
# Get the modified, added and moved python and notebook files.
IFS=$'\n' read -r -d '' -a format_files < \
<(git diff --name-only --diff-filter=MAR "${rev}" -- \
'*.py' ':(exclude)*_pb2.py')
'*.py' '*.ipynb' ':(exclude)*_pb2.py')
else
echo -e "Formatting all python files." >&2
echo -e "Formatting all python and notebook files." >&2
IFS=$'\n' read -r -d '' -a format_files < \
<(git ls-files '*.py' ':(exclude)*_pb2.py')
<(git ls-files '*.py' '*.ipynb' ':(exclude)*_pb2.py')
fi

if (( ${#format_files[@]} == 0 )); then
echo -e "\033[32mNo files to format.\033[0m"
exit 0
fi

# Exclude __init__.py files from isort targets
# Apply isort only on python files with the exception of __init__.py files
declare -a isort_files=()
for f in "${format_files[@]}"; do
if [[ "${f##*/}" != __init__.py ]]; then
if [[ "${f}" == *.py && "${f##*/}" != __init__.py ]]; then
isort_files+=("${f}")
fi
done
Expand Down
2 changes: 1 addition & 1 deletion dev_tools/requirements/deps/format.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
black==25.1.0
black[jupyter]==25.1.0
isort[colors]~=6.0.1