Skip to content

Commit

Permalink
Improved parse recovery for ternary expressions that are missing an `…
Browse files Browse the repository at this point in the history
…else` or a post-else expression. This addresses microsoft/pylance-release#3635.
  • Loading branch information
msfterictraut committed Nov 16, 2022
1 parent e9d29ce commit 8b2f908
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions packages/pyright-internal/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3014,16 +3014,17 @@ export class Parser {
}

if (!this._consumeTokenIfKeyword(KeywordType.Else)) {
return this._handleExpressionParseError(
ErrorExpressionCategory.MissingElse,
Localizer.Diagnostic.expectedElse()
return TernaryNode.create(
ifExpr,
testExpr,
this._handleExpressionParseError(
ErrorExpressionCategory.MissingElse,
Localizer.Diagnostic.expectedElse()
)
);
}

const elseExpr = this._parseTestExpression(/* allowAssignmentExpression */ true);
if (elseExpr.nodeType === ParseNodeType.Error) {
return elseExpr;
}

return TernaryNode.create(ifExpr, testExpr, elseExpr);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test('Metaclass9', () => {

test('AssignmentExpr1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['assignmentExpr1.py']);
TestUtils.validateResults(analysisResults, 5);
TestUtils.validateResults(analysisResults, 7);
});

test('AssignmentExpr2', () => {
Expand Down

0 comments on commit 8b2f908

Please sign in to comment.