Skip to content

Commit

Permalink
Implement as_packages for forbidden contracts
Browse files Browse the repository at this point in the history
Implement the funcitonality for treating the forbidden modules as a module rather than a package. If this is specified, we set the source_moduels and forbidden_modules as the package name rather than finding all downstream modules of that package.
  • Loading branch information
NicholasBunn committed Nov 8, 2024
1 parent b5ea8c7 commit 14da7ba
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/importlinter/contracts/forbidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def sort_key(module):
chains = self._get_direct_chains(source_module, forbidden_module, graph, self.as_packages)
else:
chains = graph.find_shortest_chains(
importer=source_module.name, imported=forbidden_module.name
importer=source_module.name,
imported=forbidden_module.name,
as_packages=self.as_packages,
)
if chains:
is_kept = False
Expand Down Expand Up @@ -200,8 +202,16 @@ def _get_direct_chains(
as_packages: bool,
) -> set[tuple[str, ...]]:
chains: set[tuple[str, ...]] = set()
source_modules = self._get_all_modules_in_package(source_package, graph)
forbidden_modules = self._get_all_modules_in_package(forbidden_package, graph)
source_modules = (
self._get_all_modules_in_package(source_package, graph)
if as_packages
else {source_package}
)
forbidden_modules = (
self._get_all_modules_in_package(forbidden_package, graph)
if as_packages
else {forbidden_package}
)
for source_module in source_modules:
imported_module_names = graph.find_modules_directly_imported_by(source_module.name)
for imported_module_name in imported_module_names:
Expand Down

0 comments on commit 14da7ba

Please sign in to comment.