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

Conversation

SauravMaheshkar
Copy link

Following up from #7256

This is the 1st PR updating the scripts and the requirements. Follow up PR with the actual changes will follow.

CC: @pavoljuhas

Output

Formatting all python files.
Sorting imports with isort... (version: 6.0.1)
Running the black formatter... (version: black, 25.1.0 (compiled: yes))
All done! ✨ 🍰 ✨
1162 files would be left unchanged.
..... # diff from ipynb files
Oh no! 💥 💔 💥
57 files would be reformatted, 14 files would be left unchanged.

@SauravMaheshkar
Copy link
Author

SauravMaheshkar commented May 16, 2025

If I understand the errors correctly, I need to implement a similar logic in dev_tools/bash_scripts_test.py wherein I split black for python scripts and notebooks separately?

The tests were failing because I passed the --ipynb flag for a python file. Tests are passing (atleast locally) now.

🐕 ❯ pytest -p no:warnings dev_tools/bash_scripts_test.py::test_incremental_format_branch_selection
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.11.11, pytest-8.3.5, pluggy-1.5.0
rootdir: /Users/sauravmaheshkar/dev/Cirq
configfile: pyproject.toml
collected 1 item

dev_tools/bash_scripts_test.py .                                                                                                                                            [100%]

================================================================================ 1 passed in 3.83s ================================================================================

@SauravMaheshkar SauravMaheshkar force-pushed the saurav/black-jupyter-script branch from a49cdcd to 74b272e Compare May 16, 2025 19:05
@mpharrigan mpharrigan requested review from pavoljuhas and removed request for mpharrigan May 16, 2025 21:26
Copy link

codecov bot commented May 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.67%. Comparing base (f3410e2) to head (de503d1).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7342      +/-   ##
==========================================
- Coverage   98.67%   98.67%   -0.01%     
==========================================
  Files        1112     1112              
  Lines       97038    97038              
==========================================
- Hits        95757    95755       -2     
- Misses       1281     1283       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pavoljuhas pavoljuhas self-assigned this May 20, 2025
@@ -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.

Comment on lines +99 to +105
for f in "${format_files[@]}"; do
if [[ "${f}" == *.ipynb ]]; then
format_ipynb_files+=("${f}")
else
format_py_files+=("${f}")
fi
done
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.

format_py_files+=("${f}")
fi
done

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.

Comment on lines -136 to +153
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
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.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

The default operation of this script is to check formatting of files that changed w/r to the main branch. This currently does not detect .ipynb files, please correct.

Also, please see inline comments for more suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants