Skip to content

feat: update fmt scripts to use black with jupyter ext #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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions check/format-incremental
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,23 @@ if (( only_changed == 1 )); then
else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if-branch above (lines 84-86) does not include .ipynb files. Please correct.

echo -e "Formatting all python 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

# Separate .py and .ipynb files
for f in "${format_files[@]}"; do
if [[ "${f}" == *.ipynb ]]; then
format_ipynb_files+=("${f}")
else
format_py_files+=("${f}")
fi
done
Comment on lines +99 to +105
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to separate .ipynb and .py files into two arrays.
Black can detect the file type of its arguments, just run it without the --ipynb option.


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 110-112 - the isort_files array would include .ipynb files if they were in format_files, but it should only contain .py files. Please adjust the condition on line 110.

# Exclude __init__.py files from isort targets
declare -a isort_files=()
for f in "${format_files[@]}"; do
Expand Down Expand Up @@ -133,8 +142,15 @@ if (( only_print == 1 )); then
args+=("--check" "--diff")
fi

black "${args[@]}" "${format_files[@]}"
BLACKSTATUS=$?
BLACKSTATUS=0
if (( "${#format_py_files[@]}" )); then
black "${args[@]}" "${format_py_files[@]}"
BLACKSTATUS=$?
fi
if (( "${#format_ipynb_files[@]}" )); then
black "${args[@]}" --ipynb "${format_ipynb_files[@]}"
BLACKSTATUS=$((BLACKSTATUS || $?))
fi
Comment on lines -136 to +153
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert back. It works OK if black is run without the --ipynb option.


if (( BLACKSTATUS || ISORTSTATUS )); then
exit 1
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