1from typing import Dict, List 2 3from torch.package.package_exporter import PackagingError 4 5 6__all__ = ["find_first_use_of_broken_modules"] 7 8 9def find_first_use_of_broken_modules(exc: PackagingError) -> Dict[str, List[str]]: 10 """ 11 Find all broken modules in a PackagingError, and for each one, return the 12 dependency path in which the module was first encountered. 13 14 E.g. broken module m.n.o was added to a dependency graph while processing a.b.c, 15 then re-encountered while processing d.e.f. This method would return 16 {'m.n.o': ['a', 'b', 'c']} 17 18 Args: 19 exc: a PackagingError 20 21 Returns: A dict from broken module names to lists of module names in the path. 22 """ 23 24 assert isinstance(exc, PackagingError), "exception must be a PackagingError" 25 uses = {} 26 broken_module_names = [ 27 m for m, attr in exc.dependency_graph.nodes.items() if attr.get("error", False) 28 ] 29 for module_name in broken_module_names: 30 path = exc.dependency_graph.first_path(module_name) 31 uses[module_name] = path 32 return uses 33