Skip to content

Commit 307a525

Browse files
committed
Enhance CI formatting checks for Python files
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.
1 parent 88ddb73 commit 307a525

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

.ci/check-format.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ for file in ${SH_SOURCES}; do
2121
done
2222
SH_MISMATCH_FILE_CNT=$(shfmt -l ${SH_SOURCES})
2323

24-
exit $((C_MISMATCH_LINE_CNT + SH_MISMATCH_FILE_CNT))
24+
PY_SOURCES=$(find "${REPO_ROOT}" | egrep "\.py$")
25+
for file in ${PY_SOURCES}; do
26+
echo "Checking Python file: ${file}"
27+
black --diff "${file}"
28+
done
29+
PY_MISMATCH_FILE_CNT=0
30+
if [ -n "${PY_SOURCES}" ]; then
31+
PY_MISMATCH_FILE_CNT=$(echo "$(black --check ${PY_SOURCES} 2>&1)" | grep -c "^would reformat ")
32+
fi
33+
34+
exit $((C_MISMATCH_LINE_CNT + SH_MISMATCH_FILE_CNT + PY_MISMATCH_FILE_CNT))

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ jobs:
505505
- uses: actions/checkout@v4
506506
- name: coding convention
507507
run: |
508-
sudo apt-get install -q=2 clang-format-18 shfmt
508+
sudo apt-get install -q=2 clang-format-18 shfmt python3-pip
509+
pip3 install black==25.1.0
509510
.ci/check-newline.sh
510511
.ci/check-format.sh
511512
shell: bash

CONTRIBUTING.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ However, participation requires adherence to fundamental ground rules:
3939
While there is some flexibility in basic style, it is crucial to stick to the current coding standards.
4040
Complex algorithmic constructs without proper comments will not be accepted.
4141
* Shell scripts must be formatted before submission. Use consistent flags across the project to ensure uniform formatting.
42+
* Python scripts must be formatted before submission. Use consistent flags across the project to ensure uniform formatting.
4243
* External pull requests should include thorough documentation in the pull request comments for consideration.
4344
* When composing documentation, code comments, and other materials in English,
4445
please adhere to the American English (`en_US`) dialect.
@@ -48,9 +49,12 @@ However, participation requires adherence to fundamental ground rules:
4849
Software requirement:
4950
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
5051
* [shfmt](https://github.com/mvdan/sh).
52+
* [black](https://github.com/psf/black) version 25.1.0.
5153

52-
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`.
53-
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}` and `shfmt -w $(find . -type f -name "*.sh")`.
54+
To maintain a uniform style across languages, run:
55+
* `clang-format -i *.{c,h}` to apply the project’s C/C++ formatting rules from the up-to-date .clang-format file.
56+
* `shfmt -w $(find . -type f -name "*.sh")` to clean and standardize all shell scripts.
57+
* `black .` to enforce a consistent, idiomatic layout for Python code.
5458

5559
## Coding Style for Shell Script
5660

@@ -65,6 +69,19 @@ Shell scripts must be clean, consistent, and portable. The following `shfmt` rul
6569
* Add spaces around redirection operators (e.g., `>`, `>>`).
6670
* Place binary operators (e.g., `&&`, `|`) on the next line when breaking lines.
6771

72+
## Coding Style for Python
73+
74+
Python scripts must be clean, consistent, and adhere to modern Python best practices. The following formatting rules are enforced project-wide using `black`:
75+
76+
* Use 4 spaces for indentation (never tabs).
77+
* Limit lines to 80 characters maximum.
78+
* Use double quotes for strings (unless single quotes avoid escaping).
79+
* Use trailing commas in multi-line constructs.
80+
* Format imports according to PEP 8 guidelines.
81+
* Use Unix-style line endings (LF).
82+
* Remove trailing whitespace at the end of lines.
83+
* Ensure files end with a newline.
84+
6885
## Coding Style for Modern C
6986

7087
This coding style is a variant of the [K&R style](https://en.wikipedia.org/wiki/Indentation_style#K&R).

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[tool.black]
2+
line-length = 80
3+
target-version = ['py39', 'py310', 'py311','py312','py313']
4+
include = '\.pyi?$'

0 commit comments

Comments
 (0)