Repository: pluto-lang/pluto (language: python)

Issue:
Version Packages
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.


# Releases
## @plutolang/cli@0.2.10

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource
-   Updated dependencies [62a0009]
    -   @plutolang/simulator-adapter@0.2.9
    -   @plutolang/static-generator@0.2.8
    -   @plutolang/pulumi-adapter@0.2.9
    -   @plutolang/base@0.2.8
    -   @plutolang/static-deducer@0.2.8
    -   @plutolang/graphviz-generator@0.2.8

## @plutolang/pulumi-adapter@0.2.9

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource
-   Updated dependencies [62a0009]
    -   @plutolang/base@0.2.8

## @plutolang/simulator-adapter@0.2.9

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource
-   Updated dependencies [62a0009]
    -   @plutolang/pluto-infra@0.2.9
    -   @plutolang/pluto@0.2.8
    -   @plutolang/base@0.2.8

## @plutolang/static-deducer@0.2.8

### Patch Changes

-   Updated dependencies [62a0009]
    -   @plutolang/base@0.2.8

## @plutolang/graphviz-generator@0.2.8

### Patch Changes

-   Updated dependencies [62a0009]
    -   @plutolang/base@0.2.8

## @plutolang/static-generator@0.2.8

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource
-   Updated dependencies [62a0009]
    -   @plutolang/base@0.2.8

## @plutolang/base@0.2.8

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource

## @plutolang/pluto@0.2.8

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource
-   Updated dependencies [62a0009]
    -   @plutolang/base@0.2.8

## @plutolang/pluto-infra@0.2.9

### Patch Changes

-   62a0009: feat: instantiate resource infrastructure classes asynchronously within the base class of each resource
-   Updated dependencies [62a0009]
    -   @plutolang/pluto@0.2.8
    -   @plutolang/base@0.2.8



Discussion / hints:

Fix patch:
diff --git a/.changeset/slimy-pots-wait.md b/.changeset/slimy-pots-wait.md
new file mode 100644
index 00000000..4f5e6f5b
--- /dev/null
+++ b/.changeset/slimy-pots-wait.md
@@ -0,0 +1,7 @@
+---
+"@plutolang/pyright-deducer": patch
+---
+
+fix(deducer): resolve deducer malfunction with imports in function body
+
+The deducer fails to operate correctly when encountering import statements inside function bodies, attempting to retrieve module symbols from these local scope imports. Since global scope is required for symbol resolution and these imports don't need extraction, the solution is to bypass import statements during the extraction process.
diff --git a/components/deducers/python-pyright/src/code-extractor.ts b/components/deducers/python-pyright/src/code-extractor.ts
index 398e6168..faf9e4cd 100644
--- a/components/deducers/python-pyright/src/code-extractor.ts
+++ b/components/deducers/python-pyright/src/code-extractor.ts
@@ -965,6 +965,16 @@ class OutsideSymbolFinder extends ParseTreeWalker {
     return Array.from(this._envVarNames);
   }
 
+  public override visitImport(): boolean {
+    // Ignore the import statement. We don't need to extract the imported modules.
+    return false;
+  }
+
+  public override visitImportFrom(): boolean {
+    // Ignore the import-from statement. We don't need to extract the imported modules.
+    return false;
+  }
+
   public override visitName(node: NameNode): boolean {
     if (node !== this.rootNode && !this.shouldIgnore(node)) {
       const symbol = this.typeEvaluator.lookUpSymbolRecursive(node, node.value, false);


Test patch:
diff --git a/components/deducers/python-pyright/src/test/code-extractor.test.ts b/components/deducers/python-pyright/src/test/code-extractor.test.ts
index 006ff8ab..ef1e1f05 100644
--- a/components/deducers/python-pyright/src/test/code-extractor.test.ts
+++ b/components/deducers/python-pyright/src/test/code-extractor.test.ts
@@ -325,6 +325,66 @@ foo()
   clean();
 });
 
+describe("Code segment extraction with import statements", () => {
+  const code = `
+def foo1():
+  import pluto_client
+
+def foo2():
+  import pluto_client as pc
+
+def foo3():
+  from pluto_client import Router
+
+def foo4():
+  from pluto_client import Router as R
+
+foo1()
+foo2()
+foo3()
+foo4()
+`;
+  let program: Program;
+  let sourceFile: SourceFile;
+  let clean: () => void;
+  let extractor: CodeExtractor;
+  let fetchedNodes: ParseNode[] = [];
+
+  beforeAll(() => {
+    ({ program, sourceFile, clean } = TestUtils.parseCode(code));
+    ({ extractor } = createTools(program, sourceFile));
+  });
+
+  afterAll(() => clean());
+
+  beforeEach(() => {
+    const walker = new NodeFetcher((node) => {
+      return (
+        node.nodeType === ParseNodeType.Call &&
+        node.leftExpression.nodeType === ParseNodeType.Name &&
+        node.leftExpression.value.startsWith("foo")
+      );
+    });
+    walker.walk(sourceFile.getParseResults()!.parseTree!);
+    fetchedNodes = walker.nodes;
+  });
+
+  test("should fetch 4 nodes", () => {
+    expect(fetchedNodes).toHaveLength(4);
+  });
+
+  for (let i = 0; i < 4; i++) {
+    const caseIdx = i;
+
+    test(`should not throw for foo${caseIdx + 1}`, () => {
+      const callNode = fetchedNodes[caseIdx] as ExpressionNode;
+      expect(() => {
+        extractor.extractExpressionRecursively(callNode, sourceFile);
+      }).not.toThrow();
+    });
+  }
+});
+
 test("should correctly extract the code segment for the closure created within local scope", () => {
   const code = `
 plain_string = "Hello, world"