示例#1
0
文件: typecheck.py 项目: wikier/beam
    def _type_check(self, type_constraint, datum, is_input):
        """Typecheck a PTransform related datum according to a type constraint.

    This function is used to optionally type-check either an input or an output
    to a PTransform.

    Args:
        type_constraint: An instance of a typehints.TypeContraint, one of the
          white-listed builtin Python types, or a custom user class.
        datum: An instance of a Python object.
        is_input: True if 'datum' is an input to a PTransform's DoFn. False
          otherwise.

    Raises:
      TypeError: If 'datum' fails to type-check according to 'type_constraint'.
    """
        datum_type = 'input' if is_input else 'output'

        try:
            check_constraint(type_constraint, datum)
        except CompositeTypeHintError as e:
            raise TypeCheckError, e.message, sys.exc_info()[2]
        except SimpleTypeHintError:
            error_msg = (
                "According to type-hint expected %s should be of type %s. "
                "Instead, received '%s', an instance of type %s." %
                (datum_type, type_constraint, datum, type(datum)))
            raise TypeCheckError, error_msg, sys.exc_info()[2]
示例#2
0
def _check_instance_type(
    type_constraint, instance, var_name=None, verbose=False):
  """A helper function to report type-hint constraint violations.

  Args:
    type_constraint: An instance of a 'TypeConstraint' or a built-in Python
      type.
    instance: The candidate object which will be checked by to satisfy
      'type_constraint'.
    var_name: If 'instance' is an argument, then the actual name for the
      parameter in the original function definition.

  Raises:
    TypeCheckError: If 'instance' fails to meet the type-constraint of
      'type_constraint'.
  """
  hint_type = (
      "argument: '%s'" % var_name if var_name is not None else 'return type')

  try:
    check_constraint(type_constraint, instance)
  except SimpleTypeHintError:
    if verbose:
      verbose_instance = '%s, ' % instance
    else:
      verbose_instance = ''
    raise TypeCheckError('Type-hint for %s violated. Expected an '
                         'instance of %s, instead found %san instance of %s.'
                         % (hint_type, type_constraint,
                            verbose_instance, type(instance)))
  except CompositeTypeHintError as e:
    raise TypeCheckError('Type-hint for %s violated: %s' % (hint_type, e))
示例#3
0
  def _type_check(self, type_constraint, datum, is_input):
    """Typecheck a PTransform related datum according to a type constraint.

    This function is used to optionally type-check either an input or an output
    to a PTransform.

    Args:
        type_constraint: An instance of a typehints.TypeContraint, one of the
          white-listed builtin Python types, or a custom user class.
        datum: An instance of a Python object.
        is_input: True if 'datum' is an input to a PTransform's DoFn. False
          otherwise.

    Raises:
      TypeError: If 'datum' fails to type-check according to 'type_constraint'.
    """
    datum_type = 'input' if is_input else 'output'

    try:
      check_constraint(type_constraint, datum)
    except CompositeTypeHintError as e:
      raise TypeCheckError, e.message, sys.exc_info()[2]
    except SimpleTypeHintError:
      error_msg = ("According to type-hint expected %s should be of type %s. "
                   "Instead, received '%s', an instance of type %s."
                   % (datum_type, type_constraint, datum, type(datum)))
      raise TypeCheckError, error_msg, sys.exc_info()[2]
示例#4
0
def _check_instance_type(type_constraint,
                         instance,
                         var_name=None,
                         verbose=False):
    """A helper function to report type-hint constraint violations.

  Args:
    type_constraint: An instance of a 'TypeConstraint' or a built-in Python
      type.
    instance: The candidate object which will be checked by to satisfy
      'type_constraint'.
    var_name: If 'instance' is an argument, then the actual name for the
      parameter in the original function definition.

  Raises:
    TypeCheckError: If 'instance' fails to meet the type-constraint of
      'type_constraint'.
  """
    hint_type = ("argument: '%s'" %
                 var_name if var_name is not None else 'return type')

    try:
        check_constraint(type_constraint, instance)
    except SimpleTypeHintError:
        if verbose:
            verbose_instance = '%s, ' % instance
        else:
            verbose_instance = ''
        raise TypeCheckError(
            'Type-hint for %s violated. Expected an '
            'instance of %s, instead found %san instance of %s.' %
            (hint_type, type_constraint, verbose_instance, type(instance)))
    except CompositeTypeHintError as e:
        raise TypeCheckError('Type-hint for %s violated: %s' % (hint_type, e))