def _collect_classes(<EOL>self, package_paths: Sequence[str], recurse_subpackages: bool = True<EOL>) -> Sequence[type]:
import uqbar.apis<EOL>classes = []<EOL>initial_source_paths: Set[str] = set()<EOL>for path in package_paths:<EOL><INDENT>try:<EOL><INDENT>module = importlib.import_module(path)<EOL>if hasattr(module, "<STR_LIT>"):<EOL><INDENT>initial_source_paths.update(getattr(module, "<STR_LIT>"))<EOL><DEDENT>else:<EOL><INDENT>initial_source_paths.add(module.__file__)<EOL><DEDENT><DEDENT>except ModuleNotFoundError:<EOL><INDENT>path, _, class_name = path.rpartition("<STR_LIT:.>")<EOL>module = importlib.import_module(path)<EOL>classes.append(getattr(module, class_name))<EOL><DEDENT><DEDENT>for source_path in uqbar.apis.collect_source_paths(<EOL>initial_source_paths, recurse_subpackages=recurse_subpackages<EOL>):<EOL><INDENT>package_path = uqbar.apis.source_path_to_package_path(source_path)<EOL>module = importlib.import_module(package_path)<EOL>for name in dir(module):<EOL><INDENT>if name.startswith("<STR_LIT:_>"):<EOL><INDENT>continue<EOL><DEDENT>object_ = getattr(module, name)<EOL>if isinstance(object_, type) and object_.__module__ == module.__name__:<EOL><INDENT>classes.append(object_)<EOL><DEDENT><DEDENT><DEDENT>return sorted(classes, key=lambda x: (x.__module__, x.__name__))<EOL>
Collect all classes defined in/under ``package_paths``.