Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-file-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ jobs:
- name: Run Yapf on the Python changed files
run: |
set +e
yapf --parallel --diff --style=google \
yapf --parallel --diff \
${{needs.Changes.outputs.python_files}} > diff.out 2>&1
exit_code=$?
if [[ -s ./diff.out ]]; then
Expand Down
2 changes: 2 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[style]
based_on_style = google
4 changes: 2 additions & 2 deletions scripts/format_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# limitations under the License.
# ==============================================================================
echo "Doing python language formatting..."
python3 -m yapf --style=google --in-place --recursive ./benchmarks
python3 -m yapf --style=google --in-place --recursive ./tensorflow_quantum
python3 -m yapf --in-place --recursive ./benchmarks
python3 -m yapf --in-place --recursive ./tensorflow_quantum
echo -e "Done! \nDoing notebook formatting..."
python3 ./scripts/format_ipynb.py
echo -e "Done! \nDoing C++ formatting..."
Expand Down
2 changes: 1 addition & 1 deletion scripts/format_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ for changed_file in ${changed_files}; do
)
if [[ "${changed_line_ranges}" != "--lines=0-0 " ]]; then
# Do the formatting.
results=$(yapf --style=google --diff "${changed_file}" ${changed_line_ranges})
results=$(yapf --diff "${changed_file}" ${changed_line_ranges})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

For consistency with scripts/format_all.sh and to ensure the correct environment is used, it is recommended to invoke yapf using python3 -m yapf. This avoids potential issues where the yapf executable in the PATH might be associated with a different Python interpreter or version than the one intended for the project.

Suggested change
results=$(yapf --diff "${changed_file}" ${changed_line_ranges})
results=$(python3 -m yapf --diff "${changed_file}" ${changed_line_ranges})


# Print colorized error messages.
if [ ! -z "${results}" ]; then
Expand Down
3 changes: 1 addition & 2 deletions scripts/format_ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
lines = cell.get('source')
# This will safely skip over cells containing !% magic
try:
fmt_lines = yapf.yapf_api.FormatCode(''.join(lines),
style_config="google")[0]
fmt_lines = yapf.yapf_api.FormatCode(''.join(lines))[0]

Check warning on line 31 in scripts/format_ipynb.py

View workflow job for this annotation

GitHub Actions / Check Python lint

C0103: Constant name "fmt_lines" doesn't conform to '(([A-Z_][A-Z0-9_]*)|(__.*__))$' pattern ('(([A-Z_][A-Z0-9_]*)|(__.*__))$' pattern) (invalid-name)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

When using the yapf API without an explicit style_config, it is best practice to provide the filename argument. This allows yapf to search for the nearest configuration file (like the newly added .style.yapf) relative to the file being formatted, making the script more robust if run from different directories or if different parts of the project eventually require different styles.

Suggested change
fmt_lines = yapf.yapf_api.FormatCode(''.join(lines))[0]
fmt_lines = yapf.yapf_api.FormatCode(''.join(lines), filename=fname)[0]

except (SyntaxError, yapf.yapflib.errors.YapfError):
continue
# google style always adds an EOF newline; undo this.
Expand Down
Loading