Skip to content

Commit 352dec1

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

10 files changed

Lines changed: 142 additions & 149 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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,48 +112,40 @@ 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-
123-
# Color the output if it goes to a terminal or GitHub Actions log.
124-
arg_color=()
125-
if [[ -t 1 || "${CI}" == true ]]; then
126-
arg_color=("--color")
127-
fi
128-
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")
115+
# Run ruff for import sorting.
116+
# We do this first so that if it changes files, the formatting step will pick them up.
117+
echo "Running ruff check (isort)..."
118+
check_args=("check" "--select" "I" "${arg_color[@]}")
119+
if (( only_print == 0 )); then
120+
check_args+=("--fix")
121+
else
122+
# In check mode, we want to see diffs if possible, but ruff check usually just prints violations.
123+
# To match previous behavior of "checking" without modifying, we don't add --fix.
124+
# If we want diffs for imports, --diff can be used.
125+
check_args+=("--diff")
136126
fi
137127

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

144-
BLACKVERSION=$(black --version | head -1)
145-
146-
echo "Running the black formatter... (version: $BLACKVERSION)"
147-
148-
args=("${arg_color[@]}")
136+
echo "Running ruff format..."
137+
format_args=("format" "${arg_color[@]}")
149138
if (( only_print == 1 )); then
150-
args+=("--check" "--diff")
139+
format_args+=("--check" "--diff")
151140
fi
152141

153-
black "${args[@]}" "${format_files[@]}"
154-
BLACKSTATUS=$?
142+
RUFF_FORMAT_STATUS=0
143+
if (( "${#format_files[@]}" )); then
144+
ruff "${format_args[@]}" "${format_files[@]}"
145+
RUFF_FORMAT_STATUS=$?
146+
fi
155147

156-
if (( BLACKSTATUS || ISORTSTATUS )); then
148+
if (( RUFF_CHECK_STATUS || RUFF_FORMAT_STATUS )); then
157149
exit 1
158150
fi
159151
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)