def test_subclass_of_tuple(self): self.assertTrue(subclass_of(Tuple[int, int], Tuple[int, ...])) self.assertTrue(subclass_of(Tuple[int, int], Tuple[object, ...])) self.assertTrue(subclass_of(Tuple[A, B], Tuple[A, ...])) self.assertTrue(subclass_of(Tuple[int, int], Tuple[int, int])) self.assertTrue(subclass_of(Tuple[int, int], Tuple[object, int])) self.assertTrue(not subclass_of(Tuple[int, int], Tuple[str, int])) self.assertTrue(not subclass_of(Tuple[int, int], Tuple[int, int, int])) self.assertTrue(not subclass_of(Tuple[int, int, int], Tuple[int, int]))
def __subclasscheck__(self, subclass: type) -> bool: # If an instance of type subclass is an instance of self, then subclass # is a sub class of self. self_sig = self.signature() other_sig = Something.like(subclass).signature() for attr in self_sig: if attr in other_sig: attr_sig = other_sig[attr] if (not isinstance(subclass.__dict__[attr], staticmethod) and not isinstance(subclass.__dict__[attr], classmethod) and subclass_of(attr_sig, Callable)): # The attr must be a regular method or class method, so the # first parameter should be ignored. args, rt = get_args_and_return_type(attr_sig) attr_sig = Callable[list(args[1:]), rt] if not subclass_of(attr_sig, self_sig[attr]): return False return True
def __getitem__(self, item: Any) -> Any: """ Return the value of the first encounter of a key for which ``is_instance(item, key)`` holds ``True``. :param item: any item. :return: the value of which the type corresponds with item. """ item_type = get_type(item, use_union=True) for key, value in self.items(): if subclass_of(item_type, key): return value raise KeyError('No match for {}'.format(item))
def test_subclass_of_union(self): self.assertTrue(subclass_of(F, Union[int, str])) self.assertTrue(subclass_of(F, Union[A, int])) self.assertTrue(subclass_of(F, Union[A, B])) self.assertTrue(not subclass_of(int, Union[A, B])) self.assertTrue(subclass_of(F, Optional[A])) self.assertTrue(subclass_of(NoneType, Optional[A]))
def test_union_subclass_of(self): if sys.version_info[1] in (5, 6): with self.assertRaises(TypeError): self.assertTrue(subclass_of(Union[int, A, B, F], Union[C, D])) else: self.assertTrue(subclass_of(Union[int, F], A)) self.assertTrue(subclass_of(Union[B, F], A)) self.assertTrue(not subclass_of(Union[A, B], C)) self.assertTrue(not subclass_of(Union[A, B], Union[C, D])) self.assertTrue(subclass_of(Union[int, A, B, F], Union[C, D]))
def test_subclass_of_multiple(self): self.assertTrue(subclass_of(F, A)) self.assertTrue(subclass_of(F, str)) self.assertTrue(subclass_of(F, A, str)) self.assertTrue(not subclass_of(F, A, str, int))
def test_subclass_of(self): self.assertTrue(not subclass_of(int, str)) self.assertTrue(subclass_of(E, A)) self.assertTrue(subclass_of(str, object)) self.assertTrue(subclass_of(list, List)) self.assertTrue(subclass_of(List[int], List[int])) self.assertTrue(not subclass_of(List[int], List[str])) self.assertTrue( subclass_of(List[List[List[int]]], List[List[List[int]]])) self.assertTrue(subclass_of(List[int], List[object])) self.assertTrue(not subclass_of(List[object], List[int])) self.assertTrue(subclass_of(List[Unknown], List[int])) self.assertTrue(not subclass_of('test', str))