Skip to content

Fix more typing issues - #7388

Merged
dstrain115 merged 3 commits into
quantumlib:mainfrom
dstrain115:mypy_untyped_defs2
May 30, 2025
Merged

dstrain115 merged 3 commits into
quantumlib:mainfrom
dstrain115:mypy_untyped_defs2

Conversation

@dstrain115

Copy link
Copy Markdown
Collaborator
  • This is the result of adding "-> None" to a bunch of tests and fixing all the issues that result.

- This is the result of adding "-> None" to a bunch of tests
and fixing all the issues that result.
@dstrain115
dstrain115 requested review from a team and vtomole as code owners May 29, 2025 19:12
@dstrain115
dstrain115 requested a review from mpharrigan May 29, 2025 19:12
@github-actions github-actions Bot added the size: XL lines changed >1000 label May 29, 2025
@codecov

codecov Bot commented May 29, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (b0a1751) to head (2b03c42).
Report is 9 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7388   +/-   ##
=======================================
  Coverage   98.68%   98.68%           
=======================================
  Files        1112     1112           
  Lines       97623    97703   +80     
=======================================
+ Hits        96340    96420   +80     
  Misses       1283     1283           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pavoljuhas

Copy link
Copy Markdown
Collaborator

We can narrow the # type: ignore comments to specific errors - please consider this patch:

diff --git a/cirq-core/cirq/circuits/circuit_operation_test.py b/cirq-core/cirq/circuits/circuit_operation_test.py
index af236f2e6..947cd8470 100644
--- a/cirq-core/cirq/circuits/circuit_operation_test.py
+++ b/cirq-core/cirq/circuits/circuit_operation_test.py
@@ -62,3 +62,3 @@ def test_circuit_type() -> None:
     with pytest.raises(TypeError, match='Expected circuit of type FrozenCircuit'):
-        _ = cirq.CircuitOperation(circuit)  # type: ignore
+        _ = cirq.CircuitOperation(circuit)  # type: ignore[arg-type]
 
@@ -197,3 +197,3 @@ def test_with_qubits() -> None:
     with pytest.raises(TypeError, match='must be a function or dict'):
-        _ = op_base.with_qubit_mapping('bad arg')  # type: ignore
+        _ = op_base.with_qubit_mapping('bad arg')  # type: ignore[arg-type]
 
diff --git a/cirq-core/cirq/circuits/frozen_circuit_test.py b/cirq-core/cirq/circuits/frozen_circuit_test.py
index caa4f4200..52b40900f 100644
--- a/cirq-core/cirq/circuits/frozen_circuit_test.py
+++ b/cirq-core/cirq/circuits/frozen_circuit_test.py
@@ -92,3 +92,3 @@ def test_immutable() -> None:
     ):
-        c.moments = (cirq.Moment(cirq.H(q)), cirq.Moment(cirq.X(q)))  # type: ignore
+        c.moments = (cirq.Moment(cirq.H(q)), cirq.Moment(cirq.X(q)))  # type: ignore[misc]
 
diff --git a/cirq-core/cirq/circuits/text_diagram_drawer_test.py b/cirq-core/cirq/circuits/text_diagram_drawer_test.py
index 1a5e8b9e9..ae3b1c955 100644
--- a/cirq-core/cirq/circuits/text_diagram_drawer_test.py
+++ b/cirq-core/cirq/circuits/text_diagram_drawer_test.py
@@ -240,5 +240,5 @@ def test_drawer_copy() -> None:
     }
-    orig_drawer = TextDiagramDrawer(**kwargs)  # type: ignore
+    orig_drawer = TextDiagramDrawer(**kwargs)  # type: ignore[arg-type]
 
-    same_drawer = TextDiagramDrawer(**kwargs)  # type: ignore
+    same_drawer = TextDiagramDrawer(**kwargs)  # type: ignore[arg-type]
     assert orig_drawer == same_drawer
@@ -338,3 +338,3 @@ AB D
 
-    vstacked = TextDiagramDrawer.vstack((dd, d), padding_resolver=max)  # type: ignore
+    vstacked = TextDiagramDrawer.vstack((dd, d), padding_resolver=max)  # type: ignore[arg-type]
     expected = """
@@ -352,3 +352,3 @@ A  B
 
-    hstacked = TextDiagramDrawer.hstack((d, dd), padding_resolver=max)  # type: ignore
+    hstacked = TextDiagramDrawer.hstack((d, dd), padding_resolver=max)  # type: ignore[arg-type]
     expected = """
@@ -362,3 +362,3 @@ AB D
 
-    vstacked_min = TextDiagramDrawer.vstack((dd, d), padding_resolver=min)  # type:ignore
+    vstacked_min = TextDiagramDrawer.vstack((dd, d), padding_resolver=min)  # type: ignore[arg-type]
     expected = """
@@ -376,3 +376,3 @@ AB
 
-    hstacked_min = TextDiagramDrawer.hstack((d, dd), padding_resolver=min)  # type:ignore
+    hstacked_min = TextDiagramDrawer.hstack((d, dd), padding_resolver=min)  # type: ignore[arg-type]
     expected = """
diff --git a/cirq-core/cirq/contrib/custom_simulators/custom_state_simulator_test.py b/cirq-core/cirq/contrib/custom_simulators/custom_state_simulator_test.py
index 3fd5f2fb8..bc5730948 100644
--- a/cirq-core/cirq/contrib/custom_simulators/custom_state_simulator_test.py
+++ b/cirq-core/cirq/contrib/custom_simulators/custom_state_simulator_test.py
@@ -71,3 +71,3 @@ def test_basis_state_simulator() -> None:
     assert r.measurements == {'a': np.array([1]), 'b': np.array([2])}
-    assert r._final_simulator_state._state.basis == [2, 2]  # type: ignore
+    assert r._final_simulator_state._state.basis == [2, 2]  # type: ignore[attr-defined]
 
@@ -81,3 +81,3 @@ def test_built_in_states() -> None:
     assert np.allclose(
-        r._final_simulator_state._state._state_vector,  # type: ignore
+        r._final_simulator_state._state._state_vector,  # type: ignore[attr-defined]
         [[0, 0, 0], [0, 0, 0], [0, 0, 1]],
@@ -108,3 +108,3 @@ def test_noise() -> None:
     assert r.measurements == {'a': np.array([2]), 'b': np.array([2])}
-    assert r._final_simulator_state._state.basis == [1, 2]  # type: ignore
+    assert r._final_simulator_state._state.basis == [1, 2]  # type: ignore[attr-defined]
 
diff --git a/cirq-core/cirq/ops/clifford_gate_test.py b/cirq-core/cirq/ops/clifford_gate_test.py
index 0e63a1b12..ecded719a 100644
--- a/cirq-core/cirq/ops/clifford_gate_test.py
+++ b/cirq-core/cirq/ops/clifford_gate_test.py
@@ -739,3 +739,3 @@ def test_clifford_gate_from_tableau() -> None:
     with pytest.raises(ValueError, match="Input argument has to be a CliffordTableau instance."):
-        cirq.CliffordGate.from_clifford_tableau(1)  # type: ignore
+        cirq.CliffordGate.from_clifford_tableau(1)  # type: ignore[arg-type]
 
diff --git a/cirq-core/cirq/ops/common_gate_families_test.py b/cirq-core/cirq/ops/common_gate_families_test.py
index f8bc7b74e..080113aa1 100644
--- a/cirq-core/cirq/ops/common_gate_families_test.py
+++ b/cirq-core/cirq/ops/common_gate_families_test.py
@@ -61,5 +61,5 @@ def test_any_integer_power_gate_family() -> None:
     with pytest.raises(ValueError, match='subclass of `cirq.EigenGate`'):
-        cirq.AnyIntegerPowerGateFamily(gate=cirq.testing.SingleQubitGate)  # type: ignore
+        cirq.AnyIntegerPowerGateFamily(gate=cirq.testing.SingleQubitGate)  # type: ignore[arg-type]
     with pytest.raises(ValueError, match='subclass of `cirq.EigenGate`'):
-        cirq.AnyIntegerPowerGateFamily(gate=CustomXPowGate())  # type: ignore
+        cirq.AnyIntegerPowerGateFamily(gate=CustomXPowGate())  # type: ignore[arg-type]
     eq = cirq.testing.EqualsTester()
@@ -67,3 +67,5 @@ def test_any_integer_power_gate_family() -> None:
     eq.add_equality_group(gate_family)
-    eq.add_equality_group(cirq.AnyIntegerPowerGateFamily(cirq.EigenGate))  # type: ignore
+    eq.add_equality_group(
+        cirq.AnyIntegerPowerGateFamily(cirq.EigenGate)  # type: ignore[type-abstract]
+    )
     cirq.testing.assert_equivalent_repr(gate_family)
diff --git a/cirq-core/cirq/ops/fourier_transform_test.py b/cirq-core/cirq/ops/fourier_transform_test.py
index 0d0066603..fd490197f 100644
--- a/cirq-core/cirq/ops/fourier_transform_test.py
+++ b/cirq-core/cirq/ops/fourier_transform_test.py
@@ -109,4 +109,4 @@ def test_qft() -> None:
     np.testing.assert_allclose(
-        cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1),  # type:ignore
-        arr,  # type: ignore
+        cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1),  # type: ignore[operator]
+        arr,  # type: ignore[arg-type]
         atol=1e-8,
@@ -123,3 +123,3 @@ def test_inverse() -> None:
     a, b, c = cirq.LineQubit.range(3)
-    assert cirq.qft(a, b, c, inverse=True) == cirq.qft(a, b, c) ** -1  # type:ignore
+    assert cirq.qft(a, b, c, inverse=True) == cirq.qft(a, b, c) ** -1  # type: ignore[operator]
     assert cirq.qft(a, b, c, inverse=True, without_reverse=True) == cirq.inverse(
diff --git a/cirq-core/cirq/ops/gate_operation_test.py b/cirq-core/cirq/ops/gate_operation_test.py
index d7dcf129f..d9c3b62b9 100644
--- a/cirq-core/cirq/ops/gate_operation_test.py
+++ b/cirq-core/cirq/ops/gate_operation_test.py
@@ -379,5 +379,5 @@ def test_mul() -> None:
     r = GateRMul().on(q)
-    assert 2 * r == 3  # type: ignore
+    assert 2 * r == 3  # type: ignore[operator]
     with pytest.raises(TypeError):
-        _ = r * 2  # type: ignore
+        _ = r * 2  # type: ignore[operator]
 
@@ -385,9 +385,9 @@ def test_mul() -> None:
     m = GateMul().on(q)
-    assert m * 2 == 5  # type: ignore
+    assert m * 2 == 5  # type: ignore[operator]
     with pytest.raises(TypeError):
-        _ = 2 * m  # type: ignore
+        _ = 2 * m  # type: ignore[operator]
 
     # Handles the symmetric type case correctly.
-    assert m * m == 6  # type: ignore
-    assert r * r == 4  # type: ignore
+    assert m * m == 6  # type: ignore[operator]
+    assert r * r == 4  # type: ignore[operator]
 
diff --git a/cirq-core/cirq/ops/identity_test.py b/cirq-core/cirq/ops/identity_test.py
index d5be1933f..f7141fe29 100644
--- a/cirq-core/cirq/ops/identity_test.py
+++ b/cirq-core/cirq/ops/identity_test.py
@@ -180,3 +180,3 @@ def test_identity_global() -> None:
         # The user forgot to expand the list for example.
-        cirq.identity_each(qubits)  # type: ignore
+        cirq.identity_each(qubits)  # type: ignore[arg-type]
 
diff --git a/cirq-core/cirq/ops/op_tree_test.py b/cirq-core/cirq/ops/op_tree_test.py
index bf33d4818..74b55f381 100644
--- a/cirq-core/cirq/ops/op_tree_test.py
+++ b/cirq-core/cirq/ops/op_tree_test.py
@@ -51,7 +51,7 @@ def test_flatten_op_tree() -> None:
     with pytest.raises(TypeError):
-        _ = list(cirq.flatten_op_tree(None))  # type: ignore
+        _ = list(cirq.flatten_op_tree(None))  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        _ = list(cirq.flatten_op_tree(5))  # type: ignore
+        _ = list(cirq.flatten_op_tree(5))  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        _ = list(cirq.flatten_op_tree([operations[0], (4,)]))  # type: ignore
+        _ = list(cirq.flatten_op_tree([operations[0], (4,)]))  # type: ignore[list-item]
 
@@ -70,7 +70,7 @@ def test_flatten_to_ops_or_moments() -> None:
     with pytest.raises(TypeError):
-        _ = list(cirq.flatten_to_ops_or_moments(None))  # type: ignore
+        _ = list(cirq.flatten_to_ops_or_moments(None))  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        _ = list(cirq.flatten_to_ops_or_moments(5))  # type: ignore
+        _ = list(cirq.flatten_to_ops_or_moments(5))  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        _ = list(cirq.flatten_to_ops_or_moments([operations[0], (4,)]))  # type: ignore
+        _ = list(cirq.flatten_to_ops_or_moments([operations[0], (4,)]))  # type: ignore[list-item]
 
@@ -99,7 +99,7 @@ def test_freeze_op_tree() -> None:
     with pytest.raises(TypeError):
-        cirq.freeze_op_tree(None)  # type: ignore
+        cirq.freeze_op_tree(None)  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        cirq.freeze_op_tree(5)  # type: ignore
+        cirq.freeze_op_tree(5)  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        _ = cirq.freeze_op_tree([operations[0], (4,)])  # type: ignore
+        _ = cirq.freeze_op_tree([operations[0], (4,)])  # type: ignore[list-item]
 
@@ -108,5 +108,5 @@ def test_transform_bad_tree() -> None:
     with pytest.raises(TypeError):
-        _ = list(cirq.transform_op_tree(None))  # type: ignore
+        _ = list(cirq.transform_op_tree(None))  # type: ignore[arg-type]
     with pytest.raises(TypeError):
-        _ = list(cirq.transform_op_tree(5))  # type: ignore
+        _ = list(cirq.transform_op_tree(5))  # type: ignore[arg-type]
     with pytest.raises(TypeError):
diff --git a/cirq-core/cirq/ops/qubit_order_test.py b/cirq-core/cirq/ops/qubit_order_test.py
index db129f52b..a66913ae7 100644
--- a/cirq-core/cirq/ops/qubit_order_test.py
+++ b/cirq-core/cirq/ops/qubit_order_test.py
@@ -132,2 +132,2 @@ def test_qubit_order_invalid() -> None:
     with pytest.raises(ValueError, match="Don't know how to interpret <5> as a Basis."):
-        _ = cirq.QubitOrder.as_qubit_order(5)  # type: ignore
+        _ = cirq.QubitOrder.as_qubit_order(5)  # type: ignore[arg-type]
diff --git a/cirq-core/cirq/ops/raw_types_test.py b/cirq-core/cirq/ops/raw_types_test.py
index 19c0cffd1..e023e7c8b 100644
--- a/cirq-core/cirq/ops/raw_types_test.py
+++ b/cirq-core/cirq/ops/raw_types_test.py
@@ -65,3 +65,3 @@ def test_wrapped_qid() -> None:
     assert ValidQubit('a').with_dimension(3).with_dimension(4) == ValidQubit('a').with_dimension(4)
-    assert ValidQubit('a').with_dimension(3).qubit == ValidQubit('a')  # type: ignore
+    assert ValidQubit('a').with_dimension(3).qubit == ValidQubit('a')  # type: ignore[attr-defined]
     assert ValidQubit('a').with_dimension(3) == ValidQubit('a').with_dimension(3)
@@ -76,3 +76,3 @@ def test_wrapped_qid() -> None:
 
-    assert ValidQubit('zz').with_dimension(3)._json_dict_() == {  # type: ignore
+    assert ValidQubit('zz').with_dimension(3)._json_dict_() == {  # type: ignore[attr-defined]
         'qubit': ValidQubit('zz'),
@@ -212,3 +212,3 @@ def test_default_validation_and_inverse() -> None:
     assert cirq.decompose(i) == [cirq.X(a), cirq.S(b) ** -1, cirq.Z(a)]
-    assert [*i._decompose_()] == [cirq.X(a), cirq.S(b) ** -1, cirq.Z(a)]  # type: ignore
+    assert [*i._decompose_()] == [cirq.X(a), cirq.S(b) ** -1, cirq.Z(a)]  # type: ignore[misc]
     gate = i.gate

@pavoljuhas pavoljuhas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruff finds some more functions in the touched files that lack return annotations (ANN201):

$ ruff check --select=ANN201 $(git diff --name-only --merge-base main)
...
Found 24 errors.
No fixes available (12 hidden fixes can be enabled with the `--unsafe-fixes` option).

Otherwise LGTM.

@dstrain115
dstrain115 requested a review from pavoljuhas May 30, 2025 16:41
@dstrain115

Copy link
Copy Markdown
Collaborator Author

@pavoljuhas Applied your patch.

Will do remaining functions in a later PR. This one is already big enough.

@dstrain115
dstrain115 added this pull request to the merge queue May 30, 2025
Merged via the queue into quantumlib:main with commit 086b03e May 30, 2025
@dstrain115
dstrain115 deleted the mypy_untyped_defs2 branch May 30, 2025 17:42
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
- This is the result of adding "-> None" to a bunch of tests and fixing
all the issues that result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: XL lines changed >1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants