Skip to content

Commit af8592b

Browse files
authored
Ruff formatting and fixes that are compatible with current checks (#1817)
Part of #1807 In #1806 , we fixed a lot of things identified by ruff. This PR introduces the configuration and finishes the rest of them. This PR includes formatting changes preferred by `ruff format` for which unmodified `check/format-incremental` is also happy with. The ruff formatter is mostly compatible with `black`, please see https://docs.astral.sh/ruff/formatter/black/ This PR disabled `flynt`. I don't think this is a high-value automation, we can do it with future ruff checks if we want, and it kept trying to mangle an fstring in shell_tools that required python 3.12+ (and there's no simple way to disable it for that line) cc @fdmalone @mhucka ----- this also revealed https://github.com/quantumlib/Qualtran/pull/1810/changes#r2835413664 which I'll open an issue for if there isn't a simple resolution
1 parent 9b1bacf commit af8592b

106 files changed

Lines changed: 283 additions & 88 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

check/format-incremental

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ if (( ${#format_files[@]} == 0 )); then
9595
exit 0
9696
fi
9797

98-
flynt_args=("--quiet" "-f")
99-
if (( only_print == 1 )); then
100-
flynt_args+=("--dry-run")
101-
fi
102-
103-
flynt "${format_files[@]}" "${flynt_args[@]}"
104-
FLYNTSTATUS=$?
98+
#flynt_args=("--quiet" "-f")
99+
#if (( only_print == 1 )); then
100+
# flynt_args+=("--dry-run")
101+
#fi
102+
#
103+
#flynt "${format_files[@]}" "${flynt_args[@]}"
104+
FLYNTSTATUS=0
105105

106106
isort_args=()
107107
if (( only_print == 1 )); then

dev_tools/qualtran_dev_tools/all_call_graph.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""Generate the library-wide call graph from all bloq examples."""
16+
1617
import logging
1718
import warnings
1819
from typing import Iterable

dev_tools/qualtran_dev_tools/make_reference_docs/_components/render_docstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _write_yields(f, part: DocstringSectionYields, level: int):
167167
f.write('\n')
168168

169169
else:
170-
subpart: DocstringYield
170+
param: DocstringYield
171171
for param in part.value:
172172
pname = param.name if param.name else 'yld'
173173
f.write(f'{pname}\n')

dev_tools/qualtran_dev_tools/make_reference_docs/_make.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
# ruff: noqa: E402
1416
import ast
1517
import warnings
1618
from collections import defaultdict

dev_tools/qualtran_dev_tools/notebook_execution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def __call__(self, nb_rel_path: Path, sourceroot: Path) -> _NotebookRunResult:
215215
start = time.time()
216216
err = execute_and_export_notebook(paths)
217217
end = time.time()
218-
print(f"Exported {nb_rel_path} in {end-start:.2f} seconds.")
218+
print(f"Exported {nb_rel_path} in {end - start:.2f} seconds.")
219219
return _NotebookRunResult(paths.nb_in, err, duration_s=end - start)
220220

221221

dev_tools/qualtran_dev_tools/shell_tools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def run(
9191
if abbreviate_non_option_arguments:
9292
cmd_desc = abbreviate_command_arguments_after_switches(cmd_desc)
9393
print("run:", cmd_desc, file=sys.stderr)
94-
return subprocess.run(args, **subprocess_run_kwargs) # pylint: disable=subprocess-run-check
94+
return subprocess.run( # pylint: disable=subprocess-run-check # noqa: PLW1510
95+
args, **subprocess_run_kwargs
96+
)
9597

9698

9799
def output_of(args: Union[str, List[str]], **kwargs) -> str:

pyproject.toml

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ typing = [
9393
]
9494

9595
lint = [
96-
"pylint~=3.3.1",
96+
"pylint~=3.3.1",
97+
"ruff~=0.15.0",
9798

9899
# for checking _test.py files
99100
"pytest",
@@ -111,6 +112,7 @@ format = [
111112
"flynt~=0.60",
112113
"black~=24.8.0",
113114
"isort~=5.10.1",
115+
"ruff~=0.15.0",
114116
]
115117

116118
doc = [
@@ -131,6 +133,72 @@ skip-string-normalization = true
131133
skip-magic-trailing-comma = true
132134
exclude = "qualtran/protos/*"
133135

136+
[tool.ruff]
137+
line-length = 100
138+
target-version = "py311"
139+
exclude = [
140+
"qualtran/protos/*",
141+
"*_pb2.py",
142+
"*.ipynb",
143+
]
144+
145+
[tool.ruff.lint]
146+
select = [
147+
"E", # pycodestyle
148+
"F", # pyflakes
149+
# "I", # isort (disabled until we switch formatters)
150+
"W", # pycodestyle warnings
151+
"PL", # pylint
152+
]
153+
ignore = [
154+
# From the default ruleset:
155+
"E741", # ambiguous variable name 'l', 'O', or 'I'
156+
"E743", # ambiguous function definition
157+
"E731", # assigning a lambda expression to a variable
158+
159+
# Handled by the formatter
160+
"E501", # line too long (let ruff format handle this)
161+
"W291", # trailing whitespace
162+
"W293", # blank line contains whitespace
163+
164+
# We productively use these
165+
"PLC0415", # import outside top-level
166+
"PLW2901", # loop variable overwritten
167+
"PLC0206", # Extracting value from dictionary without calling .items()
168+
"PLW0406", # Module import itself
169+
170+
# Pylint 'refactor' rules
171+
"PLR0913", # Too many arguments
172+
"PLR0912", # Too many branches
173+
"PLR0915", # Too many statements
174+
"PLR0914", # Too many local variables
175+
"PLR2004", # Magic value used in comparison
176+
"PLR0911", # Too many return statements
177+
"PLR1704", # Redefining argument with the local name
178+
"PLR1714", # Consider merging multiple comparisons
179+
"PLR1730", # replace if with min, max. Can be enabled.
180+
181+
# Might be enabled at some point
182+
"PLR0402" # Prevalent in the rotation_synthesis submodule.
183+
]
184+
185+
[tool.ruff.lint.per-file-ignores]
186+
"__init__.py" = [
187+
# Ruff wants us to redundantly re-export symbols in __init__.py files, but the
188+
# automated fix isn't in the stable release yet.
189+
"F401", # Unused import
190+
]
191+
192+
193+
[tool.ruff.lint.isort]
194+
order-by-type = false
195+
force-to-top = []
196+
split-on-trailing-comma = false
197+
198+
[tool.ruff.format]
199+
quote-style = "preserve"
200+
skip-magic-trailing-comma = true
201+
134202
[tool.isort]
135203
profile = 'black'
136204
order_by_type = false

qualtran/_infra/bloq.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ def on(self, *qubits: 'cirq.Qid') -> 'cirq.Operation':
633633
return cirq.Gate.on(BloqAsCirqGate(bloq=self), *qubits)
634634

635635
def on_registers(
636-
self, **qubit_regs: Union['cirq.Qid', Sequence['cirq.Qid'], 'NDArray[cirq.Qid]'] # type: ignore[type-var]
636+
self,
637+
**qubit_regs: Union['cirq.Qid', Sequence['cirq.Qid'], 'NDArray[cirq.Qid]'], # type: ignore[type-var]
637638
) -> 'cirq.Operation':
638639
"""A `cirq.Operation` of this bloq operating on the given qubit registers.
639640

qualtran/_infra/composite_bloq.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""Classes for building and manipulating `CompositeBloq`."""
16+
1617
from collections.abc import Hashable
1718
from functools import cached_property
1819
from typing import (

qualtran/_infra/composite_bloq_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,10 @@ def signature(self) -> Signature:
338338
return Signature([Register('control', QBit()), Register('target', QBit(), shape=(2, 3))])
339339

340340
def build_composite_bloq(
341-
self, bb: 'BloqBuilder', control: 'Soquet', target: NDArray['Soquet'] # type: ignore[type-var]
341+
self,
342+
bb: 'BloqBuilder',
343+
control: 'Soquet',
344+
target: NDArray['Soquet'], # type: ignore[type-var]
342345
) -> Dict[str, SoquetT]:
343346
for i in range(2):
344347
for j in range(3):

0 commit comments

Comments
 (0)