def __missing__(self, types: tuple) -> Callable:
     """Find and cache the next applicable method of given types."""
     self.evaluate()
     if types in self:
         return self[types]
     groups = groupby(signature(types).__sub__, self.parents(types))
     keys = groups[min(groups)] if groups else []
     funcs = {self[key] for key in keys}
     if len(funcs) == 1:
         return self.setdefault(types, *funcs)
     msg = f"{self.__name__}: {len(keys)} methods found"  # type: ignore
     raise DispatchError(msg, types, keys)
 def __setitem__(self, types: tuple, func: Callable):
     self.clean()
     types = signature(types)
     parents = types.parents = self.parents(types)
     for key in self:
         if types < key and (not parents or parents & key.parents):
             key.parents -= parents
             key.parents.add(types)
     if any(map(subtype.subcheck, types)):
         self.get_type = get_type  # switch to slower generic type checker
     super().__setitem__(types, func)
     self.__doc__ = self.docstring
Ejemplo n.º 3
0
def test_signature():
    with pytest.raises(TypeError):
        signature([list]) <= signature([None])
Ejemplo n.º 4
0
def test_signature():
    assert signature([List]) <= signature([list])
    assert signature([list]) <= signature([List])
    assert signature([List[int]]) <= signature([list])
    assert signature([list]) <= signature([List[int]])
    assert signature([List[int]]) - signature([list]) == [0]
    assert signature([list]) - signature([List[int]]) == [1]
Ejemplo n.º 5
0
def test_signature():
    assert signature([Any, List]) == (object, list)
    assert signature([List]) <= signature([list])
    assert signature([list]) <= signature([List])
    assert signature([list]) <= signature([List[int]])
    assert signature([List[int]]) - signature([list])
    assert signature([list]) - signature([List[int]]) == (1,)
Ejemplo n.º 6
0
def test_signature():
    assert signature([Any, List]) == (object, list)
    assert signature([AnyStr]) == signature([Union[bytes, str]])
    assert signature([List]) <= signature([list])
    assert signature([list]) <= signature([List])
    assert signature([list]) <= signature([List[int]])
    assert signature([List[int]]) - signature([list])
    assert signature([list]) - signature([List[int]]) == (1,)
Ejemplo n.º 7
0
def test_signature():
    assert signature([Any, List]) == (object, list)
    assert signature([AnyStr]) == signature([Union[bytes, str]])
    assert signature([TypeVar('T')]) == signature([object])
    assert signature([int]) - signature([Union[int, float]]) == (0, )
    assert signature([List]) <= signature([list])
    assert signature([list]) <= signature([List])
    assert signature([list]) <= signature([List[int]])
    assert signature([List[int]]) - signature([list])
    assert signature([list]) - signature([List[int]]) == (1, )

    # with metaclasses:
    assert signature([type]) - signature([type]) == (0, )
    assert signature([type]) - signature([object]) == (1, )
    # using EnumMeta because it is a standard, stable, metaclass
    assert signature([enum.EnumMeta]) - signature([object]) == (2, )
    assert signature([Union[type, enum.EnumMeta]]) - signature([object]) == (
        1, )