Example #1
0
    def type_check_method(self, value):
        if self._user_type_check is not None:
            res = self._user_type_check(value)
            check.invariant(
                isinstance(res, bool) or isinstance(res, TypeCheck),
                'Invalid return type from user-defined type check on Dagster type {dagster_type} '
                'when evaluated on value of type {value_type}: expected bool or TypeCheck, got '
                '{return_type}'.format(dagster_type=self.name,
                                       value_type=type(value),
                                       return_type=type(res)),
            )
            if res is False:
                return TypeCheck(success=False)
            elif res is True:
                return TypeCheck(success=True)

            return res

        elif not isinstance(value, self.python_type):
            return TypeCheck(
                success=False,
                description=
                ('Value of type {value_type} failed type check for Dagster type {dagster_type}, '
                 'expected value to be of Python type {expected_type}.'
                 ).format(
                     value_type=type(value),
                     dagster_type=self.name,
                     expected_type=self.python_type.__name__,
                 ),
            )

        return TypeCheck(success=True)
Example #2
0
    def type_check_method(self, context, value):
        from dagster.core.definitions.events import TypeCheck

        if not isinstance(value, tuple):
            return TypeCheck(
                success=False,
                description='Value should be a tuple, got a {value_type}'.
                format(value_type=type(value)),
            )

        if len(value) != len(self.runtime_types):
            return TypeCheck(
                success=False,
                description=(
                    'Tuple with key {key} requires {n} entries, received {m} '
                    'values').format(key=self.key,
                                     n=len(self.runtime_types),
                                     m=len(value)),
            )

        for item, runtime_type in zip(value, self.runtime_types):
            item_check = runtime_type.type_check(context, item)
            if not item_check.success:
                return item_check

        return TypeCheck(success=True)
Example #3
0
    def type_check_method(self, _context: "TypeCheckContext", value: object) -> TypeCheck:
        if value is not None:
            return TypeCheck(
                success=False,
                description="Value must be None, got a {value_type}".format(value_type=type(value)),
            )

        return TypeCheck(success=True)
Example #4
0
def _fail_if_not_of_type(
    value: object, value_type: t.Type[t.Any], value_type_desc: str
) -> TypeCheck:

    if not isinstance(value, value_type):
        return TypeCheck(success=False, description=_typemismatch_error_str(value, value_type_desc))

    return TypeCheck(success=True)
Example #5
0
def _fail_if_not_of_type(value, value_type, value_type_desc):

    if not isinstance(value, value_type):
        return TypeCheck(success=False,
                         description=_typemismatch_error_str(
                             value, value_type_desc))

    return TypeCheck(success=True)
Example #6
0
    def type_check_method(self, _context, value):
        if value is not None:
            return TypeCheck(
                success=False,
                description='Value must be None, got a {value_type}'.format(value_type=type(value)),
            )

        return TypeCheck(success=True)
Example #7
0
    def type_check(_context: "TypeCheckContext", value: object) -> TypeCheck:
        if not isinstance(value, expected_python_type):
            return TypeCheck(
                success=False,
                description=(
                    f"Value of type {type(value)} failed type check for Dagster type {dagster_type_name}, "
                    f"expected value to be of Python type {expected_python_type_str}."
                ),
            )

        return TypeCheck(success=True)
Example #8
0
    def type_check_method(self, _context, value):
        if not isinstance(value, self.python_type):
            return TypeCheck(
                success=False,
                description=(
                    "Value of type {value_type} failed type check for Dagster type {dagster_type}, "
                    "expected value to be of Python type {expected_type}."
                ).format(
                    value_type=type(value), dagster_type=self.name, expected_type=self.type_str,
                ),
            )

        return TypeCheck(success=True)
Example #9
0
    def type_check_method(self, context, value):
        from dagster.core.definitions.events import TypeCheck

        if not isinstance(value, set):
            return TypeCheck(
                success=False,
                description='Value should be a set, got a{value_type}'.format(
                    value_type=type(value)),
            )

        for item in value:
            item_check = self.item_type.type_check(context, item)
            if not item_check.success:
                return item_check

        return TypeCheck(success=True)
Example #10
0
    def type_check_method(self, value):
        from dagster.core.definitions.events import TypeCheck

        if not isinstance(value, dict):
            return TypeCheck(
                success=False,
                description='Value should be a dict, got a {value_type}'.
                format(value_type=type(value)),
            )

        for key, value in value.items():
            key_check = self.key_type.type_check(key)
            if not key_check.success:
                return key_check
            value_check = self.value_type.type_check(value)
            if not value_check.success:
                return value_check

        return TypeCheck(success=True)
Example #11
0
    def type_check_method(self, context, value):
        value_check = _fail_if_not_of_type(value, list, "list")
        if not value_check.success:
            return value_check

        for item in value:
            item_check = self.inner_type.type_check(context, item)
            if not item_check.success:
                return item_check

        return TypeCheck(success=True)
Example #12
0
    def type_check(self, context: "TypeCheckContext", value: object) -> TypeCheck:
        retval = self._type_check_fn(context, value)

        if not isinstance(retval, (bool, TypeCheck)):
            raise DagsterInvariantViolationError(
                (
                    "You have returned {retval} of type {retval_type} from the type "
                    'check function of type "{type_key}". Return value must be instance '
                    "of TypeCheck or a bool."
                ).format(retval=repr(retval), retval_type=type(retval), type_key=self.key)
            )

        return TypeCheck(success=retval) if isinstance(retval, bool) else retval
Example #13
0
    def type_check(self, value):
        retval = self._type_check_fn(value)

        if not isinstance(retval, (bool, TypeCheck)):
            raise DagsterInvariantViolationError((
                'You have returned {retval} of type {retval_type} from the type '
                'check function of type "{type_key}". Return value must be instance '
                'of TypeCheck or a bool.').format(retval=repr(retval),
                                                  retval_type=type(retval),
                                                  type_key=self.key))

        return TypeCheck(
            success=retval) if isinstance(retval, bool) else retval
Example #14
0
 def type_check_method(self, context, value):
     return (
         TypeCheck(success=True) if value is None else self.inner_type.type_check(context, value)
     )
Example #15
0
 def type_check_method(self, _context: "TypeCheckContext", _value: object) -> TypeCheck:
     return TypeCheck(success=True)
Example #16
0
 def type_check_method(self, _context, _value):
     return TypeCheck(success=True)