Skip to content

Commit bee1466

Browse files
committed
Fixed a bug that led to a false positive error about an "unknown type" in certain cases when using an assignment expression (walrus operator) in a loop. This addresses #4557.
1 parent 1d461ea commit bee1466

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

packages/pyright-internal/src/analyzer/typeEvaluator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
11131113
assignTypeToExpression(
11141114
node.leftExpression,
11151115
typeResult.type,
1116-
/* isTypeIncomplete */ false,
1116+
!!typeResult.isIncomplete,
11171117
node.rightExpression,
11181118
/* ignoreEmptyContainers */ true,
11191119
/* allowAssignmentToFinalVar */ true
@@ -1126,7 +1126,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
11261126
assignTypeToExpression(
11271127
node.name,
11281128
typeResult.type,
1129-
/* isTypeIncomplete */ false,
1129+
!!typeResult.isIncomplete,
11301130
node.rightExpression,
11311131
/* ignoreEmptyContainers */ true
11321132
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This sample tests the case where an assignment expression
2+
# is used within a looping construct such that the assigned
3+
# value is initially unknown.
4+
5+
# pyright: strict
6+
7+
from typing import Iterator
8+
9+
for _ in ["1"]:
10+
old_lines: Iterator[str] = iter(["2", "3"])
11+
12+
try:
13+
while True:
14+
line = next(old_lines)
15+
count = 1
16+
if count:
17+
while True:
18+
if not (line := next(old_lines)):
19+
pass
20+
elif line.startswith(""):
21+
print(line.removeprefix(""))
22+
else:
23+
old_lines = iter([line] + list(old_lines))
24+
break
25+
26+
except StopIteration:
27+
pass

packages/pyright-internal/src/tests/typeEvaluator1.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,12 @@ test('CodeFlow7', () => {
926926
TestUtils.validateResults(analysisResults, 0);
927927
});
928928

929+
test('CodeFlow8', () => {
930+
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['codeFlow8.py']);
931+
932+
TestUtils.validateResults(analysisResults, 0);
933+
});
934+
929935
test('CapturedVariable1', () => {
930936
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['capturedVariable1.py']);
931937

0 commit comments

Comments
 (0)