Skip to content

Commit 9472172

Browse files
committed
Improve SyntaxError suggestions for common operator typos
1 parent 57d4446 commit 9472172

2 files changed

Lines changed: 497 additions & 330 deletions

File tree

Grammar/python.gram

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,11 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
801801
| in_bitwise_or
802802
| isnot_bitwise_or
803803
| is_bitwise_or
804+
| invalid_eqeqeq
804805

805806
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
807+
invalid_eqeqeq:
808+
| a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='") }
806809
noteq_bitwise_or[CmpopExprPair*]:
807810
| (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
808811
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
@@ -1313,6 +1316,9 @@ invalid_assignment:
13131316
"'%s' is an illegal expression for augmented assignment",
13141317
_PyPegen_get_expr_name(a)
13151318
)}
1319+
| star_expressions a='=' b='<' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'") }
1320+
| star_expressions a='=' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'") }
1321+
| star_expressions a='=' b='!' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'") }
13161322
invalid_ann_assign_target[expr_ty]:
13171323
| list
13181324
| tuple

0 commit comments

Comments
 (0)