예제 #1
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))
예제 #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.args[0], 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 _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_with_traceback(TypeCheckError(e.args[0]))
    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_with_traceback(TypeCheckError(error_msg))
예제 #5
0
    def type_check(self, instance):
        if not isinstance(instance, ShardedKey):
            raise typehints.CompositeTypeHintError(
                "ShardedKey type-constraint violated. Valid object instance "
                "must be of type 'ShardedKey'. Instead, an instance of '%s' "
                "was received." % (instance.__class__.__name__))

        try:
            typehints.check_constraint(self.key_type, instance.key)
        except (typehints.CompositeTypeHintError,
                typehints.SimpleTypeHintError):
            raise typehints.CompositeTypeHintError(
                "%s type-constraint violated. The type of key in 'ShardedKey' "
                "is incorrect. Expected an instance of type '%s', "
                "instead received an instance of type '%s'." %
                (repr(self), typehints._unified_repr(
                    self.key_type), instance.key.__class__.__name__))
예제 #6
0
파일: batch_test.py 프로젝트: nielm/beam
    def test_explode_rebatch(self):
        exploded = list(self.converter.explode_batch(self.batch))
        rebatched = self.converter.produce_batch(exploded)

        typehints.check_constraint(self.normalized_batch_typehint, rebatched)
        self.assertTrue(self.equality_check(self.batch, rebatched))
예제 #7
0
파일: batch_test.py 프로젝트: nielm/beam
 def test_type_check_element(self):
     for element in self.converter.explode_batch(self.batch):
         typehints.check_constraint(self.normalized_element_typehint,
                                    element)
예제 #8
0
파일: batch_test.py 프로젝트: nielm/beam
 def test_type_check(self):
     typehints.check_constraint(self.normalized_batch_typehint, self.batch)