def test_union_subclass_of(self): if sys.version_info[1] in (5, ): self.assertTrue(not subclass_of(Union[int, A, B, F], Union[C, D])) else: 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]))
def test_union_subclass_of_union(self): # Subclass holds if all elements of the first enum subclass any of # the right enum. self.assertTrue(subclass_of(Union[C, D], Union[A, B])) # int is no subclass of any of Union[A, B]. self.assertTrue(not subclass_of(Union[C, D, int], Union[A, B]))
def test_subclass_of_iterable(self): self.assertTrue(subclass_of(List[int], Iterable[int])) self.assertTrue(subclass_of(Tuple[int, int, int], Iterable[int])) self.assertTrue(subclass_of(Tuple[int, ...], Iterable[int])) self.assertTrue(subclass_of(Tuple[B, C, D], Iterable[B])) self.assertTrue(subclass_of(Tuple[B, C, D], Iterable[A])) self.assertTrue(subclass_of(List[Tuple[int, str]], Iterable[Tuple[int, str]])) self.assertTrue(subclass_of(Tuple[Tuple[int, str]], Iterable[Tuple[int, str]]))
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 discover_classes( source: Union[Path, str, Module, Iterable[Module]], signature: type = Any, # type: ignore include_privates: bool = False, in_private_modules: bool = False, raise_on_fail: bool = False, exclude: Union[type, ClsPredicate, Iterable[Union[type, ClsPredicate]]] = None ) -> List[type]: """ Discover any classes within the given source and according to the given constraints. Args: source: the source in which is searched for any classes. signature: only classes that inherit from signature are returned. include_privates: if True, private classes are included as well. in_private_modules: if True, private modules are explored as well. raise_on_fail: if True, raises an ImportError upon the first import failure. exclude: one or more types or predicates that are to be excluded from the result. Returns: a list of all discovered classes (types). """ exclude_ = _ensure_set(exclude) elements = _discover_elements(source, isclass, include_privates, in_private_modules, raise_on_fail) result = list({ cls for cls in elements if (signature is Any or subclass_of(cls, signature)) and cls not in exclude_ }) exclude_predicates = (e for e in exclude_ if isfunction(e)) for pred in exclude_predicates: result = [cls for cls in result if not pred(cls)] # type: ignore[operator] # noqa result.sort(key=lambda cls: cls.__name__) return result
def discover( source: Any = None, *, what: Any = List[type], **kwargs: dict, ) -> list: """ Convenience function for discovering types in some source. If not source is given, the directory is used in which the calling module is located. Args: source: the source in which is searched or the directory of the caller if None. what: the type that is to be discovered. **kwargs: any keyword argument that is passed on. Returns: a list of discoveries. """ source = source or here(1) delegates = [ (List[type], _discover_list), (list, _discover_list), (List, _discover_list), ] for tuple_ in delegates: type_, delegate = tuple_ if subclass_of(what, type_): return delegate(what, source, **kwargs) accepted_types = ', '.join( ['`{}`'.format(delegate) for delegate, _ in delegates]) raise ValueError('Type `{}` is not supported. This function accepts: ' '{}'.format(what, accepted_types))
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_tuple_subclass_of_list(self): self.assertTrue(not subclass_of(Tuple[int, ...], List[int]))
def test_list_subclass_of_tuple(self): self.assertTrue(not subclass_of(List[int], Tuple[int, ...])) self.assertTrue(not subclass_of(List[int], Tuple[int, int]))
def test_subclass_of_tuple(self): self.assertTrue(subclass_of(Tuple[int, int], Tuple[int, ...])) self.assertTrue(subclass_of(Tuple[int, ...], Tuple[int, ...])) self.assertTrue(subclass_of(Tuple[int, int], Tuple[object, ...])) self.assertTrue(subclass_of(Tuple[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])) self.assertTrue(not subclass_of(Tuple[int, str], Tuple[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))
def test_subclass_of_literal(self): self.assertTrue(subclass_of(int, Literal[int])) self.assertTrue(subclass_of(Any, Literal[Any])) self.assertTrue(not subclass_of(int, Literal[Any])) self.assertTrue(not subclass_of(int, Literal))