diff --git a/cirq-core/cirq/circuits/frozen_circuit_test.py b/cirq-core/cirq/circuits/frozen_circuit_test.py index 8e89332af04..01b211cad32 100644 --- a/cirq-core/cirq/circuits/frozen_circuit_test.py +++ b/cirq-core/cirq/circuits/frozen_circuit_test.py @@ -68,5 +68,9 @@ def test_immutable(): q = cirq.LineQubit(0) c = cirq.FrozenCircuit(cirq.X(q), cirq.H(q)) - with pytest.raises(AttributeError, match="can't set attribute"): + # Match one of two strings. The second one is message returned since python 3.11. + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'moments' of 'FrozenCircuit' object has no setter)", + ): c.moments = (cirq.Moment(cirq.H(q)), cirq.Moment(cirq.X(q))) diff --git a/cirq-core/cirq/devices/grid_qubit_test.py b/cirq-core/cirq/devices/grid_qubit_test.py index 6b4b3045b38..cce810c3930 100644 --- a/cirq-core/cirq/devices/grid_qubit_test.py +++ b/cirq-core/cirq/devices/grid_qubit_test.py @@ -335,23 +335,39 @@ def test_to_json(): def test_immutable(): - with pytest.raises(AttributeError, match="can't set attribute"): + # Match one of two strings. The second one is message returned since python 3.11. + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'col' of 'GridQubit' object has no setter)", + ): q = cirq.GridQubit(1, 2) q.col = 3 - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'row' of 'GridQubit' object has no setter)", + ): q = cirq.GridQubit(1, 2) q.row = 3 - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'col' of 'GridQid' object has no setter)", + ): q = cirq.GridQid(1, 2, dimension=3) q.col = 3 - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'row' of 'GridQid' object has no setter)", + ): q = cirq.GridQid(1, 2, dimension=3) q.row = 3 - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'dimension' of 'GridQid' object has no setter)", + ): q = cirq.GridQid(1, 2, dimension=3) q.dimension = 3 diff --git a/cirq-core/cirq/devices/line_qubit_test.py b/cirq-core/cirq/devices/line_qubit_test.py index cb91ef06086..6c8474f313a 100644 --- a/cirq-core/cirq/devices/line_qubit_test.py +++ b/cirq-core/cirq/devices/line_qubit_test.py @@ -242,11 +242,18 @@ def _qid_shape_(self): def test_immutable(): - with pytest.raises(AttributeError, match="can't set attribute"): + # Match one of two strings. The second one is message returned since python 3.11. + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'x' of 'LineQubit' object has no setter)", + ): q = cirq.LineQubit(5) q.x = 6 - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'x' of 'LineQid' object has no setter)", + ): q = cirq.LineQid(5, dimension=4) q.x = 6 diff --git a/cirq-core/cirq/ops/gate_operation_test.py b/cirq-core/cirq/ops/gate_operation_test.py index e37d62df183..bce07dce0e9 100644 --- a/cirq-core/cirq/ops/gate_operation_test.py +++ b/cirq-core/cirq/ops/gate_operation_test.py @@ -41,10 +41,19 @@ def test_immutable(): a, b = cirq.LineQubit.range(2) op = cirq.X(a) - with pytest.raises(AttributeError, match="can't set attribute"): + # Match one of two strings. The second one is message returned since python 3.11. + with pytest.raises( + AttributeError, + match="(can't set attribute)|" + "(property 'gate' of 'SingleQubitPauliStringGateOperation' object has no setter)", + ): op.gate = cirq.Y - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|" + "(property 'qubits' of 'SingleQubitPauliStringGateOperation' object has no setter)", + ): op.qubits = [b] diff --git a/cirq-core/cirq/ops/gateset_test.py b/cirq-core/cirq/ops/gateset_test.py index e09be035149..c7adc18c8b5 100644 --- a/cirq-core/cirq/ops/gateset_test.py +++ b/cirq-core/cirq/ops/gateset_test.py @@ -115,11 +115,21 @@ def test_invalid_gate_family(): def test_gate_family_immutable(): g = cirq.GateFamily(CustomX) - with pytest.raises(AttributeError, match="can't set attribute"): + # Match one of two strings. The second one is message returned since python 3.11. + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'gate' of 'GateFamily' object has no setter)", + ): g.gate = CustomXPowGate - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'name' of 'GateFamily' object has no setter)", + ): g.name = 'new name' - with pytest.raises(AttributeError, match="can't set attribute"): + with pytest.raises( + AttributeError, + match="(can't set attribute)|(property 'description' of 'GateFamily' object has no setter)", + ): g.description = 'new description' diff --git a/cirq-core/cirq/value/classical_data.py b/cirq-core/cirq/value/classical_data.py index be8955c324a..b203b1e0c61 100644 --- a/cirq-core/cirq/value/classical_data.py +++ b/cirq-core/cirq/value/classical_data.py @@ -39,7 +39,7 @@ class MeasurementType(enum.IntEnum): CHANNEL = 2 def __repr__(self): - return f'cirq.{str(self)}' + return f'cirq.MeasurementType.{self.name}' class ClassicalDataStoreReader(abc.ABC):