From dad1ecc5620d06d2d8e140ecc42a86b070e8988f Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Mon, 2 May 2022 14:09:02 -0700 Subject: [PATCH] Gateset: fix invalid repr when gateset is empty (#5322) Fixes #5197 @tanujkhattar --- cirq-core/cirq/ops/gateset.py | 3 ++- cirq-core/cirq/ops/gateset_test.py | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cirq-core/cirq/ops/gateset.py b/cirq-core/cirq/ops/gateset.py index 53a31b650cb..90701077e20 100644 --- a/cirq-core/cirq/ops/gateset.py +++ b/cirq-core/cirq/ops/gateset.py @@ -525,9 +525,10 @@ def _value_equality_values_(self) -> Any: def __repr__(self) -> str: name_str = f'name = "{self.name}", ' if self.name is not None else '' + gates_str = f'{self._gates_repr_str}, ' if len(self._gates_repr_str) > 0 else '' return ( f'cirq.Gateset(' - f'{self._gates_repr_str}, ' + f'{gates_str}' f'{name_str}' f'unroll_circuit_op = {self._unroll_circuit_op})' ) diff --git a/cirq-core/cirq/ops/gateset_test.py b/cirq-core/cirq/ops/gateset_test.py index 02a827d02d1..5b25053e801 100644 --- a/cirq-core/cirq/ops/gateset_test.py +++ b/cirq-core/cirq/ops/gateset_test.py @@ -255,11 +255,12 @@ def test_gateset_init(): ) -def test_gateset_repr_and_str(): - cirq.testing.assert_equivalent_repr(gateset) - assert gateset.name in str(gateset) - for gate_family in gateset.gates: - assert str(gate_family) in str(gateset) +@pytest.mark.parametrize('g', [gateset, cirq.Gateset(name='empty gateset')]) +def test_gateset_repr_and_str(g): + cirq.testing.assert_equivalent_repr(g) + assert g.name in str(g) + for gate_family in g.gates: + assert str(gate_family) in str(g) @pytest.mark.parametrize(