Skip to content

Commit d837721

Browse files
ruff - enable and fix selected RUF rules (#7883)
Adding checks for #7505 [RUF005 - collection-literal-concatenation](https://docs.astral.sh/ruff/rules/collection-literal-concatenation/) [RUF015 - unnecessary-iterable-allocation-for-first-element](https://docs.astral.sh/ruff/rules/unnecessary-iterable-allocation-for-first-element/) This resolves #7505 --------- Co-authored-by: Pavol Juhas <juhas@google.com>
1 parent 66cc4c8 commit d837721

58 files changed

Lines changed: 136 additions & 110 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.

cirq-core/cirq/_compat_test.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -584,44 +584,50 @@ def _type_repr_in_deprecated_module():
584584

585585
# this is where the deprecation error should show where the deprecated usage
586586
# has occured, which is this file
587-
_deprecation_origin = ['_compat_test.py:']
587+
_deprecation_origin = '_compat_test.py:'
588588

589589
# see cirq_compat_test_data/__init__.py for the setup code
590590
_fake_a_deprecation_msg = [
591591
f'{old_parent}.fake_a was used but is deprecated',
592592
f'Use {old_parent}.module_a instead',
593-
] + _deprecation_origin
593+
_deprecation_origin,
594+
]
594595

595596
# see cirq_compat_test_data/__init__.py for the setup code
596597
_fake_b_deprecation_msg = [
597598
f'{old_parent}.fake_b was used but is deprecated',
598599
f'Use {old_parent}.module_a.module_b instead',
599-
] + _deprecation_origin
600+
_deprecation_origin,
601+
]
600602

601603
# see cirq_compat_test_data/__init__.py for the setup code
602604
_fake_ab_deprecation_msg = [
603605
f'{old_parent}.module_a.fake_ab was used but is deprecated',
604606
f'Use {old_parent}.module_a.module_b instead',
605-
] + _deprecation_origin
607+
_deprecation_origin,
608+
]
606609

607610
# see cirq_compat_test_data/__init__.py for the setup code
608611
_fake_ops_deprecation_msg = [
609612
f'{old_parent}.fake_ops was used but is deprecated',
610613
'Use cirq.ops instead',
611-
] + _deprecation_origin
614+
_deprecation_origin,
615+
]
612616

613617

614618
# see cirq_compat_test_data/__init__.py for the setup code
615619
_fake_numpy_deprecation_msg = [
616620
f'{old_parent}.fake_numpy was used but is deprecated',
617621
'Use numpy instead',
618-
] + _deprecation_origin
622+
_deprecation_origin,
623+
]
619624

620625
# see cirq_compat_test_data/__init__.py for the setup code
621626
_repeated_child_deprecation_msg = [
622627
f'{old_parent}.repeated_child was used but is deprecated',
623628
f'Use {old_parent}.repeated instead',
624-
] + _deprecation_origin
629+
_deprecation_origin,
630+
]
625631

626632

627633
def _trace_unhandled_exceptions(*args, queue: multiprocessing.Queue, func: Callable):

cirq-core/cirq/circuits/circuit_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ def test_add_op_tree(circuit_cls) -> None:
344344
a = cirq.NamedQubit('a')
345345
b = cirq.NamedQubit('b')
346346

347+
# ruff: disable[RUF005]
347348
c = circuit_cls()
348349
assert c + [cirq.X(a), cirq.Y(b)] == circuit_cls([cirq.Moment([cirq.X(a), cirq.Y(b)])])
349350

@@ -362,6 +363,7 @@ def test_radd_op_tree(circuit_cls, gate) -> None:
362363
a = cirq.NamedQubit('a')
363364
b = cirq.NamedQubit('b')
364365

366+
# ruff: disable[RUF005]
365367
c = circuit_cls()
366368
assert [gate(a), cirq.Y(b)] + c == circuit_cls([cirq.Moment([gate(a), cirq.Y(b)])])
367369

cirq-core/cirq/circuits/moment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def with_operation(self, operation: cirq.Operation) -> cirq.Moment:
218218

219219
# Use private variables to facilitate a quick copy.
220220
m = Moment(_flatten_contents=False)
221-
m._operations = self._operations + (operation,)
221+
m._operations = (*self._operations, operation)
222222
m._sorted_operations = None
223223
m._qubit_to_op = {**self._qubit_to_op, **dict.fromkeys(operation.qubits, operation)}
224224

cirq-core/cirq/circuits/moment_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def test_add() -> None:
516516
with pytest.raises(ValueError, match='Overlap'):
517517
_ = m1 + m2
518518

519+
# ruff: disable[RUF005]
519520
assert m1 + [[[[cirq.Y(b)]]]] == cirq.Moment(cirq.X(a), cirq.Y(b))
520521
assert m1 + [] == m1
521522
assert m1 + [] is m1

cirq-core/cirq/contrib/qasm_import/_lexer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def __init__(self):
4949
'ARROW',
5050
'EQ',
5151
'AND',
52-
] + list(reserved.values())
52+
*reserved.values(),
53+
]
5354

5455
def t_newline(self, t):
5556
r"""\n+"""

cirq-core/cirq/contrib/qasm_import/_parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,9 +1125,7 @@ def p_if(self, p):
11251125
for i, key in enumerate(carg):
11261126
v = (val >> i) & 1
11271127
conditions.append(sympy.Eq(sympy.Symbol(key), v))
1128-
p[0] = [
1129-
ops.ClassicallyControlledOperation(conditions=conditions, sub_operation=tuple(p[5])[0])
1130-
]
1128+
p[0] = [ops.ClassicallyControlledOperation(conditions=conditions, sub_operation=next(p[5]))]
11311129

11321130
def p_gate_params_multiple(self, p):
11331131
"""gate_params : ID ',' gate_params"""

cirq-core/cirq/contrib/qasm_import/_parser_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ def test_all_qelib_gates_unitary_equivalence(
24322432
gate = cirq_gate
24332433
expected = Circuit()
24342434
expected.append(gate.on(*qubits))
2435-
imported = list(parsed_qasm.circuit.all_operations())[0].gate
2435+
imported = next(parsed_qasm.circuit.all_operations()).gate
24362436
U_native = cirq.unitary(gate)
24372437
U_import = cirq.unitary(imported)
24382438
assert np.allclose(U_import, U_native, atol=1e-8)

cirq-core/cirq/contrib/quimb/mps_simulator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def apply_op(self, op: Any, axes: Sequence[int], prng: np.random.RandomState):
427427

428428
T = U @ self._M[n] @ self._M[p]
429429

430-
left_inds = tuple(set(T.inds) & set(self._M[n].inds)) + (new_inds[0],)
430+
left_inds = (*set(T.inds).intersection(self._M[n].inds), new_inds[0])
431431
X, Y = T.split(
432432
left_inds,
433433
method=self._simulation_options.method,

cirq-core/cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_circuits_with_readout_benchmarking_no_qubits_arg_empty_rng(mode: str) -
380380

381381
# When qubits is None, all qubits from input circuits are benchmarked as one group.
382382
assert len(readout_calibration_results) == 1
383-
qlist, result = list(readout_calibration_results.items())[0]
383+
qlist, result = next(iter(readout_calibration_results.items()))
384384
assert isinstance(qlist, tuple)
385385
assert set(qlist) == set(qubits)
386386
assert isinstance(result, SingleQubitReadoutCalibrationResult)
@@ -412,7 +412,7 @@ def test_deprecated_run_shuffled_with_readout_benchmarking() -> None:
412412
qubits=qubits,
413413
)
414414
assert len(measurements_seed) == len(input_circuits)
415-
qlist, result = list(results_seed.items())[0]
415+
qlist, result = next(iter(results_seed.items()))
416416
assert tuple(qubits) == qlist
417417
for error in result.zero_state_errors.values():
418418
assert 0.08 < error < 0.12
@@ -430,7 +430,7 @@ def test_deprecated_run_shuffled_with_readout_benchmarking() -> None:
430430
readout_repetitions=readout_repetitions,
431431
qubits=None,
432432
)
433-
qlist_none, _ = list(results_none.items())[0]
433+
qlist_none = next(iter(results_none.keys()))
434434
assert set(qlist_none) == set(qubits)
435435

436436
# Test circuit_repetitions must be > 0

cirq-core/cirq/devices/grid_qubit_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def test_diagram() -> None:
242242

243243

244244
def test_addition_subtraction() -> None:
245+
# ruff: disable[RUF005]
245246
# GridQubits
246247
assert cirq.GridQubit(1, 2) + (2, 5) == cirq.GridQubit(3, 7)
247248
assert cirq.GridQubit(1, 2) + (0, 0) == cirq.GridQubit(1, 2)
@@ -311,6 +312,7 @@ def test_addition_subtraction_numpy_array(dtype) -> None:
311312

312313

313314
def test_unsupported_add() -> None:
315+
# ruff: disable[RUF005]
314316
with pytest.raises(TypeError, match='1'):
315317
_ = cirq.GridQubit(1, 1) + 1 # type: ignore[operator]
316318
with pytest.raises(TypeError, match='(1,)'):

0 commit comments

Comments
 (0)