def _args_from_proto( self, proto: v2.program_pb2.Operation, *, arg_function_language: str) -> Dict[str, arg_func_langs.ARG_LIKE]: return_args = {} for arg in self.args: if arg.serialized_name not in proto.args: if arg.default: return_args[arg.constructor_arg_name] = arg.default continue elif arg.required: raise ValueError( f'Argument {arg.serialized_name} ' 'not in deserializing args, but is required.') value = arg_func_langs.arg_from_proto( proto.args[arg.serialized_name], arg_function_language=arg_function_language, required_arg_name=None if not arg.required else arg.serialized_name, ) if arg.value_func is not None: value = arg.value_func(value) if value is not None: return_args[arg.constructor_arg_name] = value return return_args
def test_double_value(): """Note: due to backwards compatibility, double_val conversion is one-way. double_val can be converted to python float, but a python float is converted into a float_val not a double_val. """ msg = v2.program_pb2.Arg() msg.arg_value.double_value = 1.0 parsed = arg_from_proto(msg, arg_function_language='') assert parsed == 1
def test_correspondence(min_lang: str, value: ARG_LIKE, proto: v2.program_pb2.Arg): msg = v2.program_pb2.Arg() json_format.ParseDict(proto, msg) min_i = LANGUAGE_ORDER.index(min_lang) for i, lang in enumerate(LANGUAGE_ORDER): if i < min_i: with pytest.raises(ValueError, match='not supported by arg_function_language'): _ = arg_to_proto(value, arg_function_language=lang) with pytest.raises(ValueError, match='Unrecognized function type'): _ = arg_from_proto(msg, arg_function_language=lang) else: parsed = arg_from_proto(msg, arg_function_language=lang) packed = json_format.MessageToDict( arg_to_proto(value, arg_function_language=lang), including_default_value_fields=True, preserving_proto_field_name=True, use_integers_for_enums=True, ) assert parsed == value assert packed == proto
def test_unsupported_function_language(): with pytest.raises(ValueError, match='Unrecognized arg_function_language'): _ = arg_to_proto(1, arg_function_language='NEVER GONNAH APPEN') with pytest.raises(ValueError, match='Unrecognized arg_function_language'): _ = arg_from_proto(None, arg_function_language='NEVER GONNAH APPEN')