Ejemplo n.º 1
0
def test_python_std_to_sdk_type():
    o = _type_helpers.python_std_to_sdk_type(_sdk_types.Types.Integer)
    assert o.to_flyte_literal_type().simple == _model_types.SimpleType.INTEGER

    o = _type_helpers.python_std_to_sdk_type([_sdk_types.Types.Boolean])
    assert o.to_flyte_literal_type(
    ).collection_type.simple == _model_types.SimpleType.BOOLEAN
Ejemplo n.º 2
0
 def python_std_to_sdk_type(self, t):
     """
     :param T t: User input.  Should be of the form: Types.Integer, [Types.Integer], {Types.String:
     Types.Integer}, etc.
     :rtype: flytekit.common.types.base_sdk_types.FlyteSdkType
     """
     if isinstance(t, list):
         if len(t) != 1:
             raise _user_exceptions.FlyteAssertion(
                 "When specifying a list type, there must be exactly one element in "
                 "the list describing the contained type.")
         return _container_types.List(_helpers.python_std_to_sdk_type(t[0]))
     elif isinstance(t, dict):
         raise _user_exceptions.FlyteAssertion(
             "Map types are not yet implemented.")
     elif isinstance(t, _base_sdk_types.FlyteSdkType):
         return t
     else:
         raise _user_exceptions.FlyteTypeException(
             type(t),
             _base_sdk_types.FlyteSdkType,
             additional_msg=
             "Should be of form similar to: Types.Integer, [Types.Integer], {Types.String: "
             "Types.Integer}",
             received_value=t,
         )
Ejemplo n.º 3
0
 def __init__(self, value, sdk_type=None, help=None):
     """
     :param T value:
     :param flytekit.common.types.base_sdk_types.FlyteSdkType sdk_type: If specified, the value provided must
         match this type exactly.  If not provided, the SDK will attempt to infer the type.  It is recommended
         this value be provided as the SDK might not always be able to infer the correct type.
     """
     super(Output, self).__init__(
         '',
         value,
         sdk_type=_type_helpers.python_std_to_sdk_type(sdk_type)
         if sdk_type else None,
         help=help)
Ejemplo n.º 4
0
 def __init__(self, sdk_type, help=None, **kwargs):
     """
     :param flytekit.common.types.base_sdk_types.FlyteSdkType sdk_type: This is the SDK type necessary to create an
         input to this workflow.
     :param Text help: An optional help string to describe the input to users.
     :param bool required: If set, default must be None
     :param T default: If this is not a required input, the value will default to this value.  Specify as a kwarg.
     """
     super(Input,
           self).__init__('',
                          _type_helpers.python_std_to_sdk_type(sdk_type),
                          help=help,
                          **kwargs)
Ejemplo n.º 5
0
    def _set_columns(self, columns):
        names_seen = set()
        for column in columns:
            if not isinstance(column, tuple):
                raise _user_exceptions.FlyteValueException(
                    column,
                    "When specifying a Schema type with a known set of columns.  Each column must be "
                    "specified as a tuple in the form ('name', type).",
                )
            if len(column) != 2:
                raise _user_exceptions.FlyteValueException(
                    column,
                    "When specifying a Schema type with a known set of columns.  Each column must be "
                    "specified as a tuple in the form ('name', type).",
                )
            name, sdk_type = column
            sdk_type = _helpers.python_std_to_sdk_type(sdk_type)

            if not isinstance(name, (str, _six.text_type)):
                additional_msg = (
                    "When specifying a Schema type with a known set of columns, the first element in"
                    " each tuple must be text.")
                raise _user_exceptions.FlyteTypeException(
                    received_type=type(name),
                    received_value=name,
                    expected_type={str, _six.text_type},
                    additional_msg=additional_msg,
                )

            if (not isinstance(sdk_type, _base_sdk_types.FlyteSdkType)
                    or sdk_type.to_flyte_literal_type()
                    not in get_supported_literal_types_to_pandas_types()):
                additional_msg = (
                    "When specifying a Schema type with a known set of columns, the second element of "
                    "each tuple must be a supported type.  Failed for column: {name}"
                    .format(name=name))
                raise _user_exceptions.FlyteTypeException(
                    expected_type=list(
                        get_supported_literal_types_to_pandas_types().keys()),
                    received_type=sdk_type,
                    additional_msg=additional_msg,
                )

            if name in names_seen:
                raise ValueError(
                    "The column name {name} was specified multiple times when instantiating the "
                    "Schema.".format(name=name))
            names_seen.add(name)

        self._sdk_columns = _collections.OrderedDict(columns)
Ejemplo n.º 6
0
    def __init__(self, name, value, sdk_type=None, help=None):
        """
        :param Text name:
        :param T value:
        :param U sdk_type: If specified, the value provided must cast to this type.  Normally should be an instance of
            flytekit.common.types.base_sdk_types.FlyteSdkType.  But could also be something like:

            list[flytekit.common.types.base_sdk_types.FlyteSdkType],
            dict[flytekit.common.types.base_sdk_types.FlyteSdkType,flytekit.common.types.base_sdk_types.FlyteSdkType],
            (flytekit.common.types.base_sdk_types.FlyteSdkType, flytekit.common.types.base_sdk_types.FlyteSdkType, ...)
        """
        if sdk_type is None:
            # This syntax didn't work for some reason: sdk_type = sdk_type or Output._infer_type(value)
            sdk_type = Output._infer_type(value)
        sdk_type = _type_helpers.python_std_to_sdk_type(sdk_type)

        self._binding_data = _interface.BindingData.from_python_std(sdk_type.to_flyte_literal_type(), value)
        self._var = _interface_models.Variable(sdk_type.to_flyte_literal_type(), help or "")
        self._name = name
Ejemplo n.º 7
0
    def apply_outputs_wrapper(task):
        if not isinstance(task, _sdk_runnable_tasks.SdkRunnableTask):
            additional_msg = \
                "Outputs can only be applied to a task. Did you forget the task decorator on method '{}.{}'?".format(
                    task.__module__,
                    task.__name__ if hasattr(task, "__name__") else "<unknown>"
                )
            raise _user_exceptions.FlyteTypeException(
                expected_type=_sdk_runnable_tasks.SdkRunnableTask,
                received_type=type(task),
                received_value=task,
                additional_msg=additional_msg)
        for k, v in _six.iteritems(kwargs):
            kwargs[k] = _interface_model.Variable(
                _type_helpers.python_std_to_sdk_type(
                    v).to_flyte_literal_type(),
                '')  # TODO: Support descriptions

        task.add_outputs(kwargs)
        return task