Skip to content

Commit d1d25f8

Browse files
authored
Bump mypy to 1.16.0 (#7407)
- Bump mypy to 1.16.0 - Remove redundant cast and ignore comments - Add casts and ignores for typing flagged by mypy-1.16.0 - Fix annotations of simulator step results Replaces #7404
1 parent 1f17390 commit d1d25f8

File tree

11 files changed

+30
-20
lines changed

11 files changed

+30
-20
lines changed

cirq-core/cirq/_compat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ class Wrapped(ModuleType):
469469
__dict__ = module.__dict__
470470

471471
# Workaround for: https://github.com/python/mypy/issues/8083
472-
__spec__ = _make_proxy_spec_property(module) # type: ignore
472+
__spec__ = _make_proxy_spec_property(module)
473473

474474
def __getattr__(self, name):
475475
if name in deprecated_attributes:
@@ -770,7 +770,7 @@ class Wrapped(ModuleType):
770770
__dict__ = parent_module.__dict__
771771

772772
# Workaround for: https://github.com/python/mypy/issues/8083
773-
__spec__ = _make_proxy_spec_property(parent_module) # type: ignore
773+
__spec__ = _make_proxy_spec_property(parent_module)
774774

775775
def __getattr__(self, name):
776776
if name == old_child:

cirq-core/cirq/contrib/quantum_volume/quantum_volume.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def sample_heavy_set(
137137
# Don't do a single large measurement gate because then the key will be one
138138
# large string. Instead, do a bunch of single-qubit measurement gates so we
139139
# preserve the qubit keys.
140-
sorted_qubits = sorted(qubits, key=key)
140+
sorted_qubits = sorted(qubits, key=key) # type: ignore[arg-type]
141141
circuit_copy = circuit + [cirq.measure(q) for q in sorted_qubits]
142142

143143
# Run the sampler to compare each output against the Heavy Set.

cirq-core/cirq/experiments/random_quantum_circuit_generation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import dataclasses
1919
import itertools
20-
from typing import Any, Callable, cast, Container, Iterable, Iterator, Sequence, TYPE_CHECKING
20+
from typing import Any, Callable, Container, Iterable, Iterator, Sequence, TYPE_CHECKING
2121

2222
import numpy as np
2323

@@ -458,8 +458,7 @@ def _pairs_from_moment(moment: cirq.Moment) -> list[QidPairT]:
458458
for op in moment.operations:
459459
if len(op.qubits) != 2:
460460
raise ValueError("Layer circuit contains non-2-qubit operations.")
461-
qpair = cast(QidPairT, op.qubits)
462-
pairs.append(qpair)
461+
pairs.append(op.qubits)
463462
return pairs
464463

465464

cirq-core/cirq/experiments/xeb_sampling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ def sample_2q_xeb_circuits(
341341
# Construct truncated-with-measurement circuits to run.
342342
tasks = _generate_sample_2q_xeb_tasks(zipped_circuits, cycle_depths)
343343
if shuffle is not None:
344-
shuffle = value.parse_random_state(shuffle)
345-
shuffle.shuffle(tasks)
344+
prng = value.parse_random_state(shuffle)
345+
prng.shuffle(tasks) # type: ignore[arg-type]
346346

347347
# Batch and run tasks.
348348
records = _execute_sample_2q_xeb_tasks_in_batches(

cirq-core/cirq/ops/classically_controlled_operation.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import AbstractSet, Any, Mapping, Sequence, TYPE_CHECKING
17+
from typing import AbstractSet, Any, cast, Mapping, Sequence, TYPE_CHECKING
1818

1919
import sympy
2020

@@ -212,13 +212,17 @@ def _with_measurement_key_mapping_(
212212
conditions = [protocols.with_measurement_key_mapping(c, key_map) for c in self._conditions]
213213
sub_operation = protocols.with_measurement_key_mapping(self._sub_operation, key_map)
214214
sub_operation = self._sub_operation if sub_operation is NotImplemented else sub_operation
215-
return sub_operation.with_classical_controls(*conditions)
215+
return cast(
216+
ClassicallyControlledOperation, sub_operation.with_classical_controls(*conditions)
217+
)
216218

217219
def _with_key_path_prefix_(self, prefix: tuple[str, ...]) -> ClassicallyControlledOperation:
218220
conditions = [protocols.with_key_path_prefix(c, prefix) for c in self._conditions]
219221
sub_operation = protocols.with_key_path_prefix(self._sub_operation, prefix)
220222
sub_operation = self._sub_operation if sub_operation is NotImplemented else sub_operation
221-
return sub_operation.with_classical_controls(*conditions)
223+
return cast(
224+
ClassicallyControlledOperation, sub_operation.with_classical_controls(*conditions)
225+
)
222226

223227
def _with_rescoped_keys_(
224228
self, path: tuple[str, ...], bindable_keys: frozenset[cirq.MeasurementKey]

cirq-core/cirq/sim/density_matrix_simulator_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,6 @@ def test_simulate_moment_steps_qudits(dtype: type[np.complexfloating], split: bo
779779
def test_simulate_moment_steps_empty_circuit(dtype: type[np.complexfloating], split: bool):
780780
circuit = cirq.Circuit()
781781
simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split)
782-
step = None
783782
for step in simulator.simulate_moment_steps(circuit):
784783
pass
785784
assert np.allclose(step.density_matrix(), np.array([[1]]))

cirq-core/cirq/sim/simulator_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ def _run(
227227
if self._can_be_in_run_prefix(self.noise)
228228
else (resolved_circuit[0:0], resolved_circuit)
229229
)
230-
step_result = None
230+
step_result: TStepResultBase | None = None
231231
for step_result in self._core_iterator(circuit=prefix, sim_state=sim_state):
232232
pass
233+
assert step_result is not None
233234

234235
general_ops = list(general_suffix.all_operations())
235236
if all(isinstance(op.gate, ops.MeasurementGate) for op in general_ops):
@@ -310,9 +311,10 @@ def sweep_prefixable(op: cirq.Operation):
310311
if self._can_be_in_run_prefix(self.noise)
311312
else (program[0:0], program)
312313
)
313-
step_result = None
314+
step_result: TStepResultBase | None = None
314315
for step_result in self._core_iterator(circuit=prefix, sim_state=sim_state):
315316
pass
317+
assert step_result is not None
316318
sim_state = step_result._sim_state
317319
yield from super().simulate_sweep_iter(suffix, params, qubit_order, sim_state)
318320

cirq-core/cirq/sim/sparse_simulator_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ def test_simulate_moment_steps(dtype: type[np.complexfloating], split: bool):
573573
def test_simulate_moment_steps_empty_circuit(dtype: type[np.complexfloating], split: bool):
574574
circuit = cirq.Circuit()
575575
simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split)
576-
step = None
577576
for step in simulator.simulate_moment_steps(circuit):
578577
pass
579578
assert np.allclose(step.state_vector(copy=True), np.array([1]))

cirq-google/cirq_google/engine/asyncio_executor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import asyncio
1818
import errno
1919
import threading
20-
from typing import Awaitable, Callable, TypeVar
20+
from typing import Awaitable, Callable, TYPE_CHECKING, TypeVar
2121

2222
import duet
2323
from typing_extensions import ParamSpec
2424

25+
if TYPE_CHECKING:
26+
import concurrent
27+
2528
R = TypeVar('R')
2629
P = ParamSpec("P")
2730

@@ -66,7 +69,9 @@ def submit(
6669
*args: Positional args to pass to func.
6770
**kwargs: Keyword args to pass to func.
6871
"""
69-
future = asyncio.run_coroutine_threadsafe(func(*args, **kwargs), self.loop)
72+
future: concurrent.futures.Future = asyncio.run_coroutine_threadsafe(
73+
func(*args, **kwargs), self.loop # type: ignore[arg-type]
74+
)
7075
return duet.AwaitableFuture.wrap(future)
7176

7277
_instance: AsyncioExecutor | None = None

cirq-rigetti/cirq_rigetti/circuit_transformers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
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+
1415
"""A collection of `CircuitTransformer` s that the client may pass to `RigettiQCSService` or
1516
`RigettiQCSSampler` as `transformer`.
1617
"""
17-
from typing import Callable, cast
18+
19+
from typing import Callable
1820

1921
from pyquil import Program
2022
from typing_extensions import Protocol
@@ -114,7 +116,7 @@ def transformer(*, circuit: cirq.Circuit) -> tuple[Program, dict[str, str]]:
114116
post_transformation_hooks=post_transformation_hooks,
115117
)
116118

117-
return cast(CircuitTransformer, transformer)
119+
return transformer
118120

119121

120122
@deprecated_cirq_rigetti_function()

0 commit comments

Comments
 (0)