Skip to content

Commit bb79f1f

Browse files
committed
improve syntaxerror message <> typo
1 parent 431174b commit bb79f1f

3 files changed

Lines changed: 413 additions & 332 deletions

File tree

Grammar/python.gram

Lines changed: 3 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_diamond_op
804805
| invalid_eqeqeq
805806

807+
invalid_diamond_op:
808+
| a='<' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'?") }
806809
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
807810
invalid_eqeqeq:
808811
| a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='?") }

Lib/test/test_syntax.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,16 @@ def test_ifexp_body_stmt_else_stmt(self):
35053505
]:
35063506
self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg)
35073507

3508+
def test_diamond_operator(self):
3509+
self._check_error(
3510+
"1 < > 2",
3511+
"Maybe you meant '!=' instead of '<>'?",
3512+
lineno=1,
3513+
end_lineno=1,
3514+
offset=3,
3515+
end_offset=6,
3516+
)
3517+
35083518
def test_triple_equal(self):
35093519
self._check_error(
35103520
"a === b",

0 commit comments

Comments
 (0)