11from __future__ import annotations
22
33from pathlib import Path
4- from typing import List , Optional , Sequence , Tuple
4+ from typing import List , Optional , Sequence , Set , Tuple
55
66from pytestarch .eval_structure .evaluable_graph import EvaluableArchitectureGraph
77from pytestarch .eval_structure .networkxgraph import NetworkxGraph , Node
@@ -31,12 +31,18 @@ def generate_graph(
3131 )
3232
3333 all_modules , ast = _get_all_ast_modules (module_path , root_path , exclusions )
34- imports = _get_imports_from_ast (ast )
3534
3635 internal_module_prefix = _get_internal_module_prefix (
3736 path_diff_between_root_and_module , root_path
3837 )
3938
39+ imports = _get_imports_from_ast (
40+ ast ,
41+ _actual_difference_between_root_and_module (path_diff_between_root_and_module ),
42+ root_path .name ,
43+ _get_all_internal_modules (all_modules , internal_module_prefix ),
44+ )
45+
4046 imports = _remove_excluded_imports (
4147 exclude_external_libraries , imports , internal_module_prefix
4248 )
@@ -78,16 +84,22 @@ def _get_internal_module_prefix(
7884) -> str :
7985 """In general, all internal modules should start with the root module name to be able to differentiate between
8086 internal and external modules. If the root and base module differ, the modules between them also need to be taken
81- into account, as not root.a.b modules are external, but root.a.b.base are internal.
87+ into account, as not- root.a.b- modules are external, but root.a.b.base-modules are internal.
8288 """
8389 internal_module_prefix = root_path .name + "."
8490
85- if path_diff_between_root_and_module != "." :
91+ if _actual_difference_between_root_and_module ( path_diff_between_root_and_module ) :
8692 internal_module_prefix += path_diff_between_root_and_module
8793
8894 return internal_module_prefix
8995
9096
97+ def _actual_difference_between_root_and_module (
98+ path_diff_between_root_and_module : str ,
99+ ) -> bool :
100+ return path_diff_between_root_and_module != "."
101+
102+
91103def _add_extra_levels_to_limit_if_root_and_module_path_differ (
92104 level_limit : Optional [int ], path_diff_between_root_and_module : str
93105) -> Optional [int ]:
@@ -97,16 +109,23 @@ def _add_extra_levels_to_limit_if_root_and_module_path_differ(
97109 if level_limit is None :
98110 return None
99111
100- if path_diff_between_root_and_module != "." :
112+ if _actual_difference_between_root_and_module ( path_diff_between_root_and_module ) :
101113 levels = len (path_diff_between_root_and_module .split ("." ))
102114 level_limit += levels
103115
104116 return level_limit
105117
106118
107- def _get_imports_from_ast (ast : List [NamedModule ]) -> Sequence [Import ]:
119+ def _get_imports_from_ast (
120+ ast : List [NamedModule ],
121+ root_prefix_relevant : bool ,
122+ root_prefix : str ,
123+ all_internal_modules : Set [str ],
124+ ) -> Sequence [Import ]:
108125 converter = ImportConverter ()
109- return converter .convert (ast )
126+ return converter .convert (
127+ ast , root_prefix_relevant , root_prefix , all_internal_modules
128+ )
110129
111130
112131def _get_all_ast_modules (
@@ -118,3 +137,9 @@ def _get_all_ast_modules(
118137 parser = Parser (file_filter , root_path )
119138 all_modules , ast = parser .parse (module_path )
120139 return all_modules , ast
140+
141+
142+ def _get_all_internal_modules (
143+ modules : List [str ], internal_module_prefix : str
144+ ) -> Set [str ]:
145+ return {m for m in modules if m .startswith (internal_module_prefix )}
0 commit comments