Ejemplo n.º 1
0
 def __getitem__(cls, arg_types):
     if not isinstance(arg_types, tuple):
         arg_types = (arg_types,)
     assert not any(isvariadic(arg_type) for arg_type in arg_types), "nested variadic types not supported"
     # switch tuple to typing.Tuple
     arg_types = tuple(typing.Tuple if arg_type is tuple else arg_type for arg_type in arg_types)
     if arg_types not in cls._type_cache:
         assert not cls.__args__, "cannot subscript a subscripted type {}".format(cls)
         assert len(arg_types) == len(cls._ast_fields), "must provide types for all params"
         new_dct = cls.__dict__.copy()
         new_dct.update({"__args__": arg_types})
         # type(cls) to handle FunsorMeta subclasses
         cls._type_cache[arg_types] = type(cls)(cls.__name__, (cls,), new_dct)
     return cls._type_cache[arg_types]
Ejemplo n.º 2
0
 def get_first_occurrence(self, *types):
     """ 
     Returns the first occurrence of a matching function 
     
     Based on `multipledispatch.Dispatcher.dispatch_iter`, which
     returns an iterator of matching functions. This method uses
     the same logic to select functions, but simply returns the first
     element of the iterator. If no matching functions are found, 
     `None` is returned.
     """
     n = len(types)
     for signature in self.ordering:
         if len(signature) == n and all(map(issubclass, types, signature)):
             result = self.funcs[signature]
             return result
         elif len(signature) and isvariadic(signature[-1]):
             if variadic_signature_matches(types, signature):
                 result = self.funcs[signature]
                 return result
     return None
Ejemplo n.º 3
0
def test_is_variadic():
    assert isvariadic(Variadic[A])
    assert not isvariadic(A)