diff --git a/python/tach/check_external.py b/python/tach/check_external.py index 5e66a08f..92ad602b 100644 --- a/python/tach/check_external.py +++ b/python/tach/check_external.py @@ -3,6 +3,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING +from tach.errors import TachError from tach.extension import check_external_dependencies, set_excluded_paths from tach.utils.external import get_module_mappings, is_stdlib_module @@ -18,6 +19,17 @@ class ExternalCheckDiagnosticts: unused_dependencies: dict[str, list[str]] +def extract_module_mappings(rename: list[str]) -> dict[str, list[str]]: + try: + return { + module: [name] for module, name in [module.split(":") for module in rename] + } + except ValueError as e: + raise TachError( + "Invalid rename format: expected format is a list of 'module:name' pairs, e.g. ['PIL:pillow']" + ) from e + + def check_external( project_root: Path, project_config: ProjectConfig, @@ -32,10 +44,15 @@ def check_external( use_regex_matching=project_config.use_regex_matching, ) + metadata_module_mappings = get_module_mappings() + if project_config.external.rename: + metadata_module_mappings.update( + extract_module_mappings(project_config.external.rename) + ) diagnostics = check_external_dependencies( project_root=str(project_root), source_roots=serialized_source_roots, - module_mappings=get_module_mappings(), + module_mappings=metadata_module_mappings, ignore_type_checking_imports=project_config.ignore_type_checking_imports, ) undeclared_dependencies_by_file = diagnostics[0] diff --git a/python/tach/cli.py b/python/tach/cli.py index 09aab412..d9cbc06f 100644 --- a/python/tach/cli.py +++ b/python/tach/cli.py @@ -223,7 +223,7 @@ def print_unused_external_dependencies( for pyproject_path, dependencies in unused_dependencies.items(): if dependencies: print( - f"{icons.WARNING} {BCOLORS.WARNING}Unused dependencies from project at {BCOLORS.OKCYAN}'{pyproject_path}'{BCOLORS.ENDC}{BCOLORS.ENDC}:" + f"{icons.WARNING} {BCOLORS.WARNING}Unused dependencies from project at {BCOLORS.OKCYAN}'{pyproject_path}'{BCOLORS.ENDC}{BCOLORS.ENDC}:" ) for dependency in dependencies: print(f"\t{BCOLORS.WARNING}{dependency}{BCOLORS.ENDC}") diff --git a/python/tach/extension.pyi b/python/tach/extension.pyi index af52e2b2..7d56438e 100644 --- a/python/tach/extension.pyi +++ b/python/tach/extension.pyi @@ -130,6 +130,7 @@ class CacheConfig: class ExternalDependencyConfig: exclude: list[str] + rename: list[str] class UnusedDependencies: path: str diff --git a/src/core/config.rs b/src/core/config.rs index 31e00a83..b01093a5 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -194,6 +194,8 @@ impl CacheConfig { pub struct ExternalDependencyConfig { #[serde(default, skip_serializing_if = "Vec::is_empty")] pub exclude: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub rename: Vec, } impl ExternalDependencyConfig {