def arg_from_proto(
    arg_proto: v2.program_pb2.Arg, *, required_arg_name: str | None = None
) -> ARG_RETURN_LIKE | None:
    """Extracts a python value from an argument value proto.

    Args:
        arg_proto: The proto containing a serialized value.
        required_arg_name: If set to `None`, the method will return `None` when
            given an unset proto value. If set to a string, the method will
            instead raise an error complaining that the value is missing in that
            situation.

    Returns:
        The deserialized value, or else None if there was no set value and
        `required_arg_name` was set to `None`.

    Raises:
        ValueError: If the arg protohas a value of an unrecognized type or is
            missing a required arg name.
    """

    which = arg_proto.WhichOneof('arg')
    match which:
        case 'arg_value':
            arg_value = arg_proto.arg_value
            which_val = arg_value.WhichOneof('arg_value')
            match which_val:
                case 'float_value':
                    result = float(arg_value.float_value)
                    if math.ceil(result) == math.floor(result):
                        return int(result)
                    return result
                case 'double_value':
                    result = float(arg_value.double_value)
                    if math.ceil(result) == math.floor(result):
                        return int(result)
                    return result
                case 'bool_value':
                    return bool(arg_value.bool_value)
                case 'bool_values':
                    return list(arg_value.bool_values.values)
                case 'string_value':
                    return str(arg_value.string_value)
                case 'int64_values':
                    return [int(v) for v in arg_value.int64_values.values]
                case 'double_values':
                    return [float(v) for v in arg_value.double_values.values]
                case 'string_values':
                    return [str(v) for v in arg_value.string_values.values]
                case 'value_with_unit':
                    return tunits.Value.from_proto(arg_value.value_with_unit)
                case 'bytes_value':
                    return bytes(arg_value.bytes_value)
                case 'complex_value':
                    return complex(
                        arg_value.complex_value.real_value, arg_value.complex_value.imag_value
                    )
                case 'tuple_value':
                    values = (
                        arg_from_proto(tuple_proto) for tuple_proto in arg_value.tuple_value.values
                    )
                    sequence_type = arg_value.tuple_value.sequence_type
                    match sequence_type:
                        case v2.program_pb2.Tuple.SequenceType.LIST:
                            return list(values)
                        case v2.program_pb2.Tuple.SequenceType.TUPLE:
                            return tuple(values)
                        case v2.program_pb2.Tuple.SequenceType.SET:
                            return set(values)
                        case v2.program_pb2.Tuple.SequenceType.FROZENSET:
                            return frozenset(values)
                    raise ValueError('Unrecognized type: {sequence_type}')  # pragma: no cover

                case 'ndarray_value':
                    return _ndarray_from_proto(arg_value)
            raise ValueError(f'Unrecognized value type: {which_val!r}')  # pragma: no cover
        case 'symbol':
            return sympy.Symbol(arg_proto.symbol)
        case 'func':
            func = _arg_func_from_proto(arg_proto.func, required_arg_name=required_arg_name)
            if func is not None:
                return func
        case 'measurement_key':
            return MeasurementKey(
                name=arg_proto.measurement_key.string_key,
                path=tuple(arg_proto.measurement_key.path),
            )

    if required_arg_name is not None:
        raise ValueError(
            f'{required_arg_name} is missing or has an unrecognized '
            f'argument type (WhichOneof("arg")={which!r}).'
        )

    return None
Extracts a python value from an argument value proto.

    Args:
        arg_proto: The proto containing a serialized value.
        required_arg_name: If set to `None`, the method will return `None` when
            given an unset proto value. If set to a string, the method will
            instead raise an error complaining that the value is missing in that
            situation.

    Returns:
        The deserialized value, or else None if there was no set value and
        `required_arg_name` was set to `None`.

    Raises:
        ValueError: If the arg protohas a value of an unrecognized type or is
            missing a required arg name.
    
cirq-google/cirq_google/serialization/arg_func_langs.py