Skip to content

Commit f495fec

Browse files
committed
[ci] Add workflow to check python code formatting.
Signed-off-by: boschmitt <[email protected]>
1 parent bbbbcc7 commit f495fec

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

.github/workflows/pr_sanity_checks.yaml

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
outputs:
2121
check-cpp: ${{ steps.filter.outputs.check-cpp }}
2222
check-all-cpp: ${{ steps.filter.outputs.check-all-cpp }}
23+
check-python: ${{ steps.filter.outputs.check-python }}
24+
check-all-python: ${{ steps.filter.outputs.check-all-python }}
2325
steps:
2426
- name: Checkout repository
2527
uses: actions/checkout@v4
@@ -32,13 +34,16 @@ jobs:
3234
with:
3335
filters: |
3436
check-all-cpp:
35-
- '.github/workflows/pr_sanity_checks.yaml'
3637
- '.clang-format'
3738
check-cpp:
3839
- '**/*.cpp'
3940
- '**/*.h'
41+
check-all-python:
42+
- '.style.yapf'
43+
check-python:
44+
- '**/*.py'
4045
41-
check-clang-format:
46+
check-cpp:
4247
name: Check C++ code formatting
4348
needs: [check-changes]
4449
if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true'
@@ -85,8 +90,7 @@ jobs:
8590
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
8691
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
8792
88-
git config clangFormat.binary clang-format-$LLVM_VERSION
89-
if ! git clang-format $DIFF_COMMIT_SHA; then
93+
if ! git clang-format-$LLVM_VERSION $DIFF_COMMIT_SHA; then
9094
git diff --ignore-submodules > clang-format.patch
9195
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
9296
exit 1
@@ -102,3 +106,92 @@ jobs:
102106
name: clang-format-patch
103107
path: clang-*.patch
104108

109+
check-python:
110+
name: Check Python code formatting
111+
needs: [check-changes]
112+
if: needs.check-changes.outputs.check-python == 'true' || needs.check-changes.outputs.check-all-python == 'true'
113+
runs-on: ubuntu-latest
114+
steps:
115+
- name: Checkout repository
116+
uses: actions/checkout@v4
117+
with:
118+
set-safe-directory: true
119+
120+
- name: Set up Python
121+
uses: actions/setup-python@v4
122+
with:
123+
python-version: '3.x'
124+
125+
- name: Install YAPF
126+
run: pip install yapf
127+
128+
- name: YAPF all things
129+
if: needs.check-changes.outputs.check-all-python == 'true'
130+
run: |
131+
git ls-files '*.py' | xargs yapf -i
132+
133+
if ! git diff --exit-code; then
134+
git diff --ignore-submodules > yapf-format.patch
135+
echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
136+
exit 1
137+
fi
138+
echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
139+
exit 0
140+
141+
- name: YAPF changed files
142+
if: needs.check-changes.outputs.check-all-python != 'true'
143+
run: |
144+
# We did a shallow clone, and thus we need to make sure to fetch the base
145+
# commit.
146+
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
147+
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
148+
149+
git diff --diff-filter=d $DIFF_COMMIT_SHA -- '*.py' | yapf-diff --i
150+
151+
if ! git diff --exit-code; then
152+
git diff --ignore-submodules > yapf-format.patch
153+
echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
154+
exit 1
155+
fi
156+
echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
157+
exit 0
158+
159+
- name: Upload format patch
160+
uses: actions/upload-artifact@v4
161+
continue-on-error: true
162+
if: ${{ failure() }}
163+
with:
164+
name: yapf-format-patch
165+
path: yapf-*.patch
166+
167+
# This job is used for branch protection checks.
168+
verify:
169+
name: Sanity check PR
170+
if: ${{ always() }}
171+
needs:
172+
- check-cpp
173+
- check-python
174+
runs-on: ubuntu-latest
175+
steps:
176+
- name: Check results
177+
run: |
178+
status="success"
179+
180+
check_result() {
181+
name=$1
182+
result=$2
183+
184+
# NOTE: "skipped" is considered success.
185+
if [[ "$result" == "failure" || "$result" == "cancelled" ]]; then
186+
echo "$name job failed"
187+
188+
status="failed"
189+
fi
190+
}
191+
192+
check_result "check-cpp" "${{needs.check-cpp.result}}"
193+
check_result "check-python" "${{needs.check-python.result}}"
194+
195+
if [[ "$status" != "success" ]]; then
196+
exit 1
197+
fi

libs/qec/lib/decoder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ decoder_result decoder::decode(const cudaqx::tensor<uint8_t> &syndrome) {
2929
// Check tensor is of order-1
3030
// If order >1, we could check that other modes are of dim = 1 such that
3131
// n x 1, or 1 x n tensors are still valid.
32-
if (syndrome.rank() != 1) {
32+
if (syndrome.rank() != 1)
33+
{
3334
throw std::runtime_error("Decode requires rank-1 tensors");
3435
}
3536
std::vector<float_t> soft_syndrome(syndrome.shape()[0]);

libs/qec/python/cudaq_qec/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@dataclass
1313
class patch:
14-
"""A logical qubit patch representation for surface code quantum error correction.
14+
"""A logical qubit patch representation for surface code quantum error correction.
1515
1616
This class represents a logical qubit encoded in a 2D patch, containing
1717
data qubits and both X and Z ancilla qubits arranged in a 2D lattice pattern.

0 commit comments

Comments
 (0)