Skip to content

Commit 5b66e74

Browse files
committed
chore:migrate form black to ruff(#916)
1 parent b9b2812 commit 5b66e74

10 files changed

Lines changed: 146 additions & 144 deletions

File tree

.github/problem-matchers/black.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

.github/problem-matchers/ruff.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "ruff",
5+
"pattern": [
6+
{
7+
"regexp": "^(.+?):(\\d+):(\\d+): (?:([A-Z0-9]+) )?(.+)$",
8+
"file": 1,
9+
"line": 2,
10+
"column": 3,
11+
"code": 4,
12+
"message": 5
13+
}
14+
]
15+
}
16+
]
17+
}

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
- name: Check format
8585
continue-on-error: ${{inputs.soft-linting == 'true'}}
8686
run: |
87-
echo '::add-matcher::.github/problem-matchers/black.json'
87+
echo '::add-matcher::.github/problem-matchers/ruff.json'
8888
check/format-incremental
8989
9090
- name: Check lint

check/format-incremental

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,48 +112,46 @@ if (( ${#format_files[@]} == 0 )); then
112112
exit 0
113113
fi
114114

115-
# Apply isort only on Python files with the exception of __init__.py files.
116-
declare -a isort_files=()
117-
for f in "${format_files[@]}"; do
118-
if [[ "${f}" == *.py && "${f##*/}" != __init__.py ]]; then
119-
isort_files+=("${f}")
120-
fi
121-
done
122-
123115
# Color the output if it goes to a terminal or GitHub Actions log.
124116
arg_color=()
125117
if [[ -t 1 || "${CI}" == true ]]; then
126118
arg_color=("--color")
127119
fi
128120

129-
ISORTVERSION=$(isort --version-number)
130-
131-
echo "Sorting imports with isort... (version: $ISORTVERSION)"
132-
133-
args=("${arg_color[@]}")
134-
if (( only_print == 1 )); then
135-
args+=("--check" "--diff")
121+
# Run ruff for import sorting.
122+
# We do this first so that if it changes files, the formatting step will pick them up.
123+
echo "Running ruff check (isort)..."
124+
check_args=("check" "--select" "I" "${arg_color[@]}")
125+
if (( only_print == 0 )); then
126+
check_args+=("--fix")
127+
else
128+
# In check mode, we want to see diffs if possible, but ruff check usually just prints violations.
129+
# To match previous behavior of "checking" without modifying, we don't add --fix.
130+
# If we want diffs for imports, --diff can be used.
131+
check_args+=("--diff")
136132
fi
137133

138-
ISORTSTATUS=0
139-
if (( "${#isort_files[@]}" )); then
140-
isort "${args[@]}" "${isort_files[@]}"
141-
ISORTSTATUS=$?
134+
RUFF_CHECK_STATUS=0
135+
if (( "${#format_files[@]}" )); then
136+
# We pass all files to ruff; it handles excluding those that shouldn't be touched if configured,
137+
# but here we are passing a specific list of files.
138+
ruff "${check_args[@]}" "${format_files[@]}"
139+
RUFF_CHECK_STATUS=$?
142140
fi
143141

144-
BLACKVERSION=$(black --version | head -1)
145-
146-
echo "Running the black formatter... (version: $BLACKVERSION)"
147-
148-
args=("${arg_color[@]}")
142+
echo "Running ruff format..."
143+
format_args=("format" "${arg_color[@]}")
149144
if (( only_print == 1 )); then
150-
args+=("--check" "--diff")
145+
format_args+=("--check" "--diff")
151146
fi
152147

153-
black "${args[@]}" "${format_files[@]}"
154-
BLACKSTATUS=$?
148+
RUFF_FORMAT_STATUS=0
149+
if (( "${#format_files[@]}" )); then
150+
ruff "${format_args[@]}" "${format_files[@]}"
151+
RUFF_FORMAT_STATUS=$?
152+
fi
155153

156-
if (( BLACKSTATUS || ISORTSTATUS )); then
154+
if (( RUFF_CHECK_STATUS || RUFF_FORMAT_STATUS )); then
157155
exit 1
158156
fi
159157
exit 0

docs/_scripts/build_api_docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515
"""Tool to generate external api_docs for qsim shameless copy from TFQ."""
16+
1617
import os
1718

1819
from absl import app, flags
@@ -24,7 +25,7 @@
2425

2526
flags.DEFINE_string(
2627
"code_url_prefix",
27-
("https://github.com/quantumlib/qsim/tree/main/" "qsimcirq"),
28+
("https://github.com/quantumlib/qsim/tree/main/qsimcirq"),
2829
"The url prefix for links to code.",
2930
)
3031

@@ -40,7 +41,6 @@
4041

4142

4243
def main(unused_argv):
43-
4444
doc_generator = generate_lib.DocGenerator(
4545
root_title="qsim",
4646
py_modules=[("qsimcirq", qs)],

0 commit comments

Comments
 (0)