Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/slimy-pots-wait.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions components/deducers/python-pyright/src/code-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions components/deducers/python-pyright/src/test/code-extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down