[modular] Do not track imports in functions (#36279)

* Add check

* just check for function

* Update examples
This commit is contained in:
Cyril Vallez
2025-02-25 10:29:47 +01:00
committed by GitHub
parent 4b5cf5496d
commit bc65f3fc1c
10 changed files with 82 additions and 33 deletions

View File

@@ -649,9 +649,11 @@ class ModuleMapper(CSTVisitor, ABC):
self.current_function = None
def visit_If(self, node):
for stmt in node.body.body:
if m.matches(stmt, m.SimpleStatementLine(body=[m.ImportFrom() | m.Import()])):
self.imports.append(node)
# If we are inside a function, do not add the import to the list of imports
if self.current_function is None:
for stmt in node.body.body:
if m.matches(stmt, m.SimpleStatementLine(body=[m.ImportFrom() | m.Import()])):
self.imports.append(node)
def visit_ClassDef(self, node: ClassDef) -> None:
"""Record class nodes to create their dependencies at the end."""