Skip to content

Commit

Permalink
Fixed recent regression that results in false positive when a inner f…
Browse files Browse the repository at this point in the history
…unction with an inferred return type is a coroutine (async) and is referenced within the function body in which it's declared. This addresses #8232 and #8229.
  • Loading branch information
erictraut committed Jun 27, 2024
1 parent f104e7b commit 5e7a12f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19878,7 +19878,19 @@ export function createTypeEvaluator(
// don't bother doing additional work.
let cacheEntry = readTypeCacheEntry(subnode);
if (cacheEntry && !cacheEntry.typeResult.isIncomplete) {
return cacheEntry.typeResult;
const typeResult = cacheEntry.typeResult;

// Handle the special case where a function or class is partially evaluated.
// Indicate that these are not complete types.
if (isFunction(typeResult.type) && FunctionType.isPartiallyEvaluated(typeResult.type)) {
return { ...typeResult, isIncomplete: true };
}

if (isClass(typeResult.type) && ClassType.isPartiallyEvaluated(typeResult.type)) {
return { ...typeResult, isIncomplete: true };
}

return typeResult;
}

callback();
Expand Down
17 changes: 17 additions & 0 deletions packages/pyright-internal/src/tests/samples/coroutines4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This sample tests the case where an inner coroutine with an inferred
# return type is referenced in a manner that results in recursion.

import asyncio


def func1(replace_inner: bool) -> None:
inner = lambda: None

async def wrapper():
inner()

wrapped_fn = wrapper()
asyncio.create_task(wrapped_fn)

if replace_inner:
inner = lambda: None
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ test('Coroutines3', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('Coroutines4', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['coroutines4.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('Loop1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['loop1.py']);

Expand Down

0 comments on commit 5e7a12f

Please sign in to comment.