Example #1
0
    def __init__(self, pb_object: Union[GeneratedProtocolMessageType, FlyteIdlEntity]):
        """
        :param Union[T, FlyteIdlEntity] pb_object:
        """
        v = pb_object
        # This section converts an existing proto object (or a subclass of) to the right type expected by this instance
        # of GenericProto. GenericProto can be used with any protobuf type (not restricted to FlyteType). This makes it
        # a bit tricky to figure out the right version of the underlying raw proto class to use to populate the final
        # struct.
        # If the provided object has to_flyte_idl(), call it to produce a raw proto.
        if isinstance(pb_object, FlyteIdlEntity):
            v = pb_object.to_flyte_idl()

        # A check to ensure the raw proto (v) is of the correct expected type. This also performs one final attempt to
        # convert it to the correct type by leveraging from_flyte_idl (implemented by all FlyteTypes) in case this class
        # is initialized with one.
        expected_type = type(self).pb_type
        if expected_type != type(v) and expected_type != type(pb_object):
            if isinstance(type(self).pb_type, FlyteType):
                v = expected_type.from_flyte_idl(v).to_flyte_idl()
            else:
                raise _user_exceptions.FlyteTypeException(
                    received_type=type(pb_object), expected_type=expected_type, received_value=pb_object
                )
        data = v.SerializeToString()
        super(Protobuf, self).__init__(
            scalar=_literals.Scalar(
                binary=_literals.Binary(value=bytes(data) if _six.PY2 else data, tag=type(self).tag)
            )
        )
Example #2
0
def test_scalar_binary():
    obj = literals.Scalar(
        binary=literals.Binary(
            b"value",
            "taggy"
        )
    )
    assert obj.primitive is None
    assert obj.error is None
    assert obj.blob is None
    assert obj.binary is not None
    assert obj.schema is None
    assert obj.none_type is None
    assert obj.binary.tag == "taggy"
    assert obj.binary.value == b"value"

    obj2 = literals.Scalar.from_flyte_idl(obj.to_flyte_idl())
    assert obj2 == obj
    assert obj2.primitive is None
    assert obj2.error is None
    assert obj2.blob is None
    assert obj2.binary is not None
    assert obj2.schema is None
    assert obj2.none_type is None
    assert obj2.binary.tag == "taggy"
    assert obj2.binary.value == b"value"
Example #3
0
 def __init__(self, pb_object):
     """
     :param T pb_object:
     """
     data = pb_object.SerializeToString()
     super(
         Protobuf,
         self).__init__(scalar=_literals.Scalar(binary=_literals.Binary(
             value=bytes(data) if _six.PY2 else data, tag=type(self).tag)))
def test_infer_proto_from_literal():
    sdk_type = _flyte_engine.FlyteDefaultTypeEngine().infer_sdk_type_from_literal(
        _literal_models.Literal(
            scalar=_literal_models.Scalar(
                binary=_literal_models.Binary(
                    value="", tag="{}{}".format(_proto.Protobuf.TAG_PREFIX, "flyteidl.core.errors_pb2.ContainerError",),
                )
            )
        )
    )
    assert sdk_type.pb_type == _errors_pb2.ContainerError