8000 Fixed a bug in the handling of `for` loops that resulted in incorrect… · nejch/scip-python@26cfeb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 26cfeb9

Browse files
committed
Fixed a bug in the handling of for loops that resulted in incorrect type inference for the target expression.
1 parent d9b22cf commit 26cfeb9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,11 +1016,18 @@ export class Binder extends ParseTreeWalker {
10161016
this._addAntecedent(preForLabel, this._currentFlowNode!);
10171017
this._currentFlowNode = preForLabel;
10181018
this._addAntecedent(preElseLabel, this._currentFlowNode);
1019-
this._createAssignmentTargetFlowNodes(node.targetExpression, /* walkTargets */ true, /* unbound */ false);
1019+
const targetExpressions = this._trackCodeFlowExpressions(() => {
1020+
this._createAssignmentTargetFlowNodes(node.targetExpression, /* walkTargets */ true, /* unbound */ false);
1021+
});
10201022

10211023
this._bindLoopStatement(preForLabel, postForLabel, () => {
10221024
this.walk(node.forSuite);
10231025
this._addAntecedent(preForLabel, this._currentFlowNode!);
1026+
1027+
// Add any target expressions since they are modified in the loop.
1028+
targetExpressions.forEach((value) => {
1029+
this._currentScopeCodeFlowExpressions?.add(value);
1030+
});
10241031
});
10251032

10261033
this._currentFlowNode = this._finishFlowLabel(preElseLabel);

packages/pyright-internal/src/tests/samples/forLoop1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ def __init__(self):
8686

8787
for b in B():
8888
...
89+
90+
def func3():
91+
x = None
92+
for x in range(1):
93+
pass
94+
95+
reveal_type(x, expected_text="int | None")

0 commit comments

Comments
 (0)
0