Skip to content

Commit

Permalink
Merge pull request #440 from gauge-sh/allow-external-dep-rename-config
Browse files Browse the repository at this point in the history
Allow configuring rename rules for external modules
  • Loading branch information
emdoyle authored Nov 26, 2024
2 parents c3997d5 + 65fb605 commit 14bf3f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion python/tach/check_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion python/tach/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions python/tach/extension.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class CacheConfig:

class ExternalDependencyConfig:
exclude: list[str]
rename: list[str]

class UnusedDependencies:
path: str
Expand Down
2 changes: 2 additions & 0 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ impl CacheConfig {
pub struct ExternalDependencyConfig {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub exclude: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub rename: Vec<String>,
}

impl ExternalDependencyConfig {
Expand Down

0 comments on commit 14bf3f6

Please sign in to comment.