예제 #1
0
    def assert_typehints_equal(self, left, right):
        left = typehints.normalize(left)
        right = typehints.normalize(right)

        if match_is_named_tuple(left):
            self.assertTrue(match_is_named_tuple(right))
            self.assertEqual(left.__annotations__, right.__annotations__)
        else:
            self.assertEqual(left, right)
예제 #2
0
파일: batch.py 프로젝트: zhoufek/beam
    def from_typehints(*, element_type, batch_type) -> 'BatchConverter':
        element_type = typehints.normalize(element_type)
        batch_type = typehints.normalize(batch_type)
        for constructor in BATCH_CONVERTER_REGISTRY:
            result = constructor(element_type, batch_type)
            if result is not None:
                return result

        # TODO(BEAM-14339): Aggregate error information from the failed
        # BatchConverter matches instead of this generic error.
        raise TypeError(
            f"Unable to find BatchConverter for element_type {element_type!r} and "
            f"batch_type {batch_type!r}")
예제 #3
0
파일: batch.py 프로젝트: mahak/beam
    def from_typehints(*, element_type, batch_type) -> 'BatchConverter':
        element_type = typehints.normalize(element_type)
        batch_type = typehints.normalize(batch_type)
        for constructor in BATCH_CONVERTER_REGISTRY:
            result = constructor(element_type, batch_type)
            if result is not None:
                return result

        # TODO(https://github.com/apache/beam/issues/21654): Aggregate error
        # information from the failed BatchConverter matches instead of this
        # generic error.
        raise TypeError(
            f"Unable to find BatchConverter for element_type {element_type!r} and "
            f"batch_type {batch_type!r}")
예제 #4
0
def get_signature(func):
    """Like inspect.signature(), but supports Py2 as well.

  This module uses inspect.signature instead of getfullargspec since in the
  latter: 'the "self" parameter is always reported, even for bound methods'
  https://github.com/python/cpython/blob/44f91c388a6f4da9ed3300df32ca290b8aa104ea/Lib/inspect.py#L1103
  """
    # Fall back on funcsigs if inspect module doesn't have 'signature'; prefer
    # inspect.signature over funcsigs.signature if both are available.
    if hasattr(inspect, 'signature'):
        inspect_ = inspect
    else:
        inspect_ = funcsigs

    try:
        signature = inspect_.signature(func)
    except ValueError:
        # Fall back on a catch-all signature.
        params = [
            inspect_.Parameter('_', inspect_.Parameter.POSITIONAL_OR_KEYWORD),
            inspect_.Parameter('__unknown__varargs',
                               inspect_.Parameter.VAR_POSITIONAL),
            inspect_.Parameter('__unknown__keywords',
                               inspect_.Parameter.VAR_KEYWORD)
        ]

        signature = inspect_.Signature(params)

    # This is a specialization to hint the first argument of certain builtins,
    # such as str.strip.
    if isinstance(func, _MethodDescriptorType):
        params = list(signature.parameters.values())
        if params[0].annotation == params[0].empty:
            params[0] = params[0].replace(annotation=func.__objclass__)
            signature = signature.replace(parameters=params)

    # This is a specialization to hint the return value of type callables.
    if (signature.return_annotation == signature.empty
            and isinstance(func, type)):
        signature = signature.replace(
            return_annotation=typehints.normalize(func))

    return signature
예제 #5
0
파일: batch_test.py 프로젝트: nielm/beam
 def setUp(self):
     self.converter = self.create_batch_converter()
     self.normalized_batch_typehint = typehints.normalize(
         self.batch_typehint)
     self.normalized_element_typehint = typehints.normalize(
         self.element_typehint)
예제 #6
0
 def __init__(self, key_type):
     typehints.validate_composite_type_param(
         key_type, error_msg_prefix='Parameter to ShardedKeyType hint')
     self.key_type = typehints.normalize(key_type)
예제 #7
0
 def __init__(self, key_type):
     self.key_type = typehints.normalize(key_type)