Skip to content

Enhance CI formatting checks for Python files #600

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 2 commits into from
Jun 15, 2025

Conversation

Lzzz666
Copy link
Contributor

@Lzzz666 Lzzz666 commented Jun 10, 2025

  • Update .ci/check-format.sh to include checks for Python files using Black.
  • Modify .github/workflows/main.yml to install Python dependencies for formatting.
  • Add Python-specific settings to .editorconfig to enforce consistent formatting rules.

This ensures that both shell scripts and Python files adhere to defined coding standards.

Summary by Bito

This pull request enhances CI formatting checks by integrating support for Python files using the Black formatter. It updates CI scripts and GitHub Actions to include Python-specific checks and dependencies, while refactoring existing Python code for improved readability and maintainability. Additionally, the JIT template generation logic has been refactored for enhanced clarity.

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Check CONTRIBUTING.md carefully and enforce the guidelines.

@jserv jserv requested a review from ChinYikMing June 10, 2025 05:58
@Lzzz666 Lzzz666 requested a review from jserv June 10, 2025 07:06
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Use git rebase -i to squash and rework git commit messages.
Enforce the rules mentioned in https://cbea.ms/git-commit/ .

@Lzzz666 Lzzz666 force-pushed the python-formatter branch from 4cd6137 to 3d9ea88 Compare June 10, 2025 15:54
Copy link
Collaborator

@ChinYikMing ChinYikMing left a comment

Choose a reason for hiding this comment

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

Add a description about Python code formatting in the following section:

## Coding Convention

CONTRIBUTING.md Outdated
@@ -48,9 +48,10 @@ However, participation requires adherence to fundamental ground rules:
Software requirement:
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
* [shfmt](https://github.com/mvdan/sh).
* [black](https://github.com/psf/black) for Python code formatting.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Simply mention black is informative.

CONTRIBUTING.md Outdated
This repository consistently contains an up-to-date `.clang-format` file with rules that match the explained ones and uses shell script formatting supported by `shfmt`.
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}` and `shfmt -w $(find . -type f -name "*.sh")`.
This repository consistently contains an up-to-date `.clang-format` file with rules that match the explained ones, uses shell script formatting supported by `shfmt`, and Python code formatting with `black`.
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}`, `shfmt -w $(find . -type f -name "*.sh")`, and `black .` for Python files.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Refine the sentence to emphasize the goal of each command for different target programming languages.

PY_MISMATCH_FILE_CNT=0
if [ -n "${PY_SOURCES}" ]; then
PY_MISMATCH_OUTPUT=$(black --check ${PY_SOURCES} 2>&1)
PY_MISMATCH_FILE_CNT=$(echo "${PY_MISMATCH_OUTPUT}" | grep -c "^would reformat ")
Copy link
Collaborator

@ChinYikMing ChinYikMing Jun 10, 2025

Choose a reason for hiding this comment

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

Combine two statements into single for simplicity:

PY_MISMATCH_FILE_CNT=$(echo "$(black --check ${PY_SOURCES} 2>&1)" | grep -c "^would reformat ")

@Lzzz666
Copy link
Contributor Author

Lzzz666 commented Jun 11, 2025

@ChinYikMing I’ve pushed the updates, please check!

CONTRIBUTING.md Outdated
Python scripts must be clean, consistent, and adhere to modern Python best practices. The following formatting rules are enforced project-wide using `black`:

* Use 4 spaces for indentation (never tabs).
* Limit lines to 88 characters maximum.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the CONTRIBUTING.md specifies an 80 characters per line limit for C code, I noticed that 88 characters are used here for Python. I'm curious - how was the number 88 chosen? Should we consider aligning the limit across languages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with your suggestion—having the same limit across languages makes sense. I initially chose 88 characters simply because that’s Black’s default.

@visitorckw
Copy link
Collaborator

I noticed that your commit message mentions several different changes in a single patch. Would it be better to split them into separate patches? For example, one patch could fix the Python code style, and another could add the CI check - instead of combining them all in one.

@Lzzz666 Lzzz666 force-pushed the python-formatter branch 2 times, most recently from cc66024 to 5108e0d Compare June 12, 2025 12:32
@Lzzz666 Lzzz666 marked this pull request as draft June 12, 2025 12:38
@Lzzz666 Lzzz666 force-pushed the python-formatter branch from 5108e0d to 7333c05 Compare June 12, 2025 12:50
@Lzzz666 Lzzz666 marked this pull request as ready for review June 12, 2025 16:44
@Lzzz666 Lzzz666 marked this pull request as draft June 12, 2025 16:45
@sysprog21 sysprog21 deleted a comment from bito-code-review bot Jun 12, 2025
@sysprog21 sysprog21 deleted a comment from bito-code-review bot Jun 12, 2025
@Lzzz666 Lzzz666 force-pushed the python-formatter branch from 7333c05 to 602bee8 Compare June 13, 2025 02:33
@sysprog21 sysprog21 deleted a comment from bito-code-review bot Jun 13, 2025
@Lzzz666 Lzzz666 marked this pull request as ready for review June 13, 2025 02:38
pyproject.toml Outdated
@@ -0,0 +1,11 @@
[tool.black]
line-length = 80
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not including py312? Just for curiosity, any reason for that (e.g., parser compatibility)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed that using Black v24.8.0 will issue an error when used with Python 3.12.5, due to an upstream memory safety issue. If we decide to support py312, should we also include py313 ? Additionally, should we pin Black to the latest v25.1.0 when installing? Since Black v24.10.0, it immediately errors in a Python 3.12.5 environment, and it requires at least Python 3.9.

pyproject.toml Outdated
line-length = 80
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
include = '\.pyi?$'
extend-exclude = '''
Copy link
Collaborator

@ChinYikMing ChinYikMing Jun 13, 2025

Choose a reason for hiding this comment

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

Under what circumstances do the .git and build directories generate Python files? If no, we can eliminate the rules in the configuration file for simplicity.

@jserv @visitorckw , can you confirm this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Under what circumstances do the .git and build directories generate Python files? If no, we can eliminate the rules in the configuration file for simplicity.

The use of Python scripts is dedicated to code generation at first glance. The formatting of Python code is not really a serious concern since it is used as one-shot tooling.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I’ve removed the extend-exclude rules from Black configuration.

Lzzz666 added 2 commits June 14, 2025 02:01
Add Black-based formatting checks for Python files:
- Update `.ci/check-format.sh` to include checks for Python files using Black 25.1.0.
- Modify `.github/workflows/main.yml` to install Black for formatting.
- Add `pyproject.toml` for configuring Black as the Python code formatter.
- Enhance `CONTRIBUTING.md` with guidelines for Python formatting.
@Lzzz666 Lzzz666 force-pushed the python-formatter branch from 602bee8 to 58e975e Compare June 13, 2025 18:05
@ChinYikMing
Copy link
Collaborator

LGTM.

@jserv jserv merged commit 56d20a2 into sysprog21:master Jun 15, 2025
55 of 64 checks passed
@jserv
Copy link
Contributor

jserv commented Jun 15, 2025

Thank @Lzzz666 for contributing!

@Lzzz666 Lzzz666 deleted the python-formatter branch June 15, 2025 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants