diff --git a/modguard/parsing/boundary.py b/modguard/parsing/boundary.py index e48f75a5..c9c54686 100644 --- a/modguard/parsing/boundary.py +++ b/modguard/parsing/boundary.py @@ -15,7 +15,10 @@ def __init__(self): def visit_ImportFrom(self, node): # Check if 'Boundary' is imported specifically from a 'modguard'-rooted module - if (node.module == "modguard" or node.module and node.module.startswith("modguard.")) and any( + is_modguard_module_import = node.module is not None and ( + node.module == "modguard" or node.module.startswith("modguard.") + ) + if is_modguard_module_import and any( alias.name == "Boundary" for alias in node.names ): self.is_modguard_boundary_imported = True diff --git a/modguard/parsing/imports.py b/modguard/parsing/imports.py index 2c87c365..10910c18 100644 --- a/modguard/parsing/imports.py +++ b/modguard/parsing/imports.py @@ -55,14 +55,14 @@ def _get_ignored_modules(self, lineno: int) -> Optional[list[str]]: def visit_ImportFrom(self, node): # For relative imports (level > 0), adjust the base module path - if node.level > 0: + if node.module is not None and node.level > 0: num_paths_to_strip = node.level - 1 if self.is_package else node.level base_path_parts = self.current_mod_path.split(".") if num_paths_to_strip: base_path_parts = base_path_parts[:-num_paths_to_strip] base_mod_path = ".".join([*base_path_parts, node.module if node.module else '']) else: - base_mod_path = node.module + base_mod_path = node.module or "" ignored_modules = self._get_ignored_modules(node.lineno) @@ -71,14 +71,21 @@ def visit_ImportFrom(self, node): return self.generic_visit(node) for name_node in node.names: - if ignored_modules is not None and ( + local_mod_path = ( f"{'.' * node.level}{node.module}.{name_node.asname or name_node.name}" - in ignored_modules - ): + if node.module + else f"{'.' * node.level}{name_node.asname or name_node.name}" + ) + if ignored_modules is not None and (local_mod_path in ignored_modules): # This import is ignored by a modguard-ignore directive continue - self.imports.append(f"{base_mod_path}.{name_node.asname or name_node.name}") + global_mod_path = ( + f"{base_mod_path}.{name_node.asname or name_node.name}" + if node.module + else (name_node.asname or name_node.name) + ) + self.imports.append(global_mod_path) self.generic_visit(node) diff --git a/modguard/parsing/public.py b/modguard/parsing/public.py index 1f4e69d9..6f34c7b2 100644 --- a/modguard/parsing/public.py +++ b/modguard/parsing/public.py @@ -14,7 +14,10 @@ def __init__(self, module_name: str): self.import_found = False def visit_ImportFrom(self, node): - if (node.module == "modguard" or node.module and node.module.startswith("modguard.")) and any( + is_modguard_module_import = node.module is not None and ( + node.module == "modguard" or node.module.startswith("modguard.") + ) + if is_modguard_module_import and any( alias.name == self.module_name for alias in node.names ): self.import_found = True @@ -37,7 +40,10 @@ def __init__(self, current_mod_path: str, is_package: bool = False): self.public_members: list[PublicMember] = [] def visit_ImportFrom(self, node): - if (node.module == "modguard" or node.module and node.module.startswith("modguard.")) and any( + is_modguard_module_import = node.module is not None and ( + node.module == "modguard" or node.module.startswith("modguard.") + ) + if is_modguard_module_import and any( alias.name == "public" for alias in node.names ): self.is_modguard_public_imported = True