Skip to content

Commit

Permalink
Fix failing tests for Python 3.11 (#6181)
Browse files Browse the repository at this point in the history
This commit addresses the failing tests in the cirq-core module when run with Python 3.11.
 The failures were due to changes in the error messages associated with the `@property` 
annotation in Python 3.11.

The changes in this commit include:
1. Updating the expected error messages in the test cases to 
   match either the old or the new error message format.
2. Modifying the `__repr__` method in the `MeasurementType` class in
   `classical_data.py` to correctly represent the enum value.

These changes ensure that the tests pass for both Python 3.11 and earlier versions.
  • Loading branch information
navaro1 committed Jul 7, 2023
1 parent 5d3e681 commit 9dff011
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
6 changes: 5 additions & 1 deletion cirq-core/cirq/circuits/frozen_circuit_test.py
Expand Up @@ -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)))
26 changes: 21 additions & 5 deletions cirq-core/cirq/devices/grid_qubit_test.py
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions cirq-core/cirq/devices/line_qubit_test.py
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions cirq-core/cirq/ops/gate_operation_test.py
Expand Up @@ -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]


Expand Down
16 changes: 13 additions & 3 deletions cirq-core/cirq/ops/gateset_test.py
Expand Up @@ -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'


Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/value/classical_data.py
Expand Up @@ -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):
Expand Down

0 comments on commit 9dff011

Please sign in to comment.