As I proposed in #151129 (comment), we can also add suggestions for <> ("not equal" in Pascal and Python 2), "=<", "=>" and "=!" (typo in "<=", ">=" and "!="), "===" ("is" in JavaScript and PHP).
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 9bf3a67939..8d2cf58616 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -801,8 +801,14 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
| in_bitwise_or
| isnot_bitwise_or
| is_bitwise_or
+ | invalid_diamond_op
+ | invalid_eqeqeq
+invalid_diamond_op:
+ | a='<' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'") }
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
+invalid_eqeqeq:
+ | a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='") }
noteq_bitwise_or[CmpopExprPair*]:
| (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
@@ -1313,6 +1319,9 @@ invalid_assignment:
"'%s' is an illegal expression for augmented assignment",
_PyPegen_get_expr_name(a)
)}
+ | star_expressions a='=' b='<' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'") }
+ | star_expressions a='=' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'") }
+ | star_expressions a='=' b='!' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'") }
invalid_ann_assign_target[expr_ty]:
| list
| tuple
with these changes got these outputs, kind of stuck with our first case @skirpichev could you please share your thoughts on this. Thanks!
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a <> b"
File "<string>", line 1
a, b = 1, 2; a <> b
^^
SyntaxError: invalid syntax
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a === b"
File "<string>", line 1
a, b = 1, 2; a === b
^^^
SyntaxError: invalid syntax. Maybe you meant '==' instead of '==='
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a =< b"
File "<string>", line 1
a, b = 1, 2; a =< b
^^
SyntaxError: invalid syntax. Maybe you meant '<=' instead of '=<'
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a => b"
File "<string>", line 1
a, b = 1, 2; a => b
^^
SyntaxError: invalid syntax. Maybe you meant '>=' instead of '=>'
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a =! b"
File "<string>", line 1
a, b = 1, 2; a =! b
^^
SyntaxError: invalid syntax. Maybe you meant '!=' instead of '=!'
Activity
Aniketsy commented on Jun 11, 2026
diff --git a/Grammar/python.gram b/Grammar/python.gram index 9bf3a67939..8d2cf58616 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -801,8 +801,14 @@ compare_op_bitwise_or_pair[CmpopExprPair*]: | in_bitwise_or | isnot_bitwise_or | is_bitwise_or + | invalid_diamond_op + | invalid_eqeqeq +invalid_diamond_op: + | a='<' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'") } eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) } +invalid_eqeqeq: + | a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='") } noteq_bitwise_or[CmpopExprPair*]: | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) } lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) } @@ -1313,6 +1319,9 @@ invalid_assignment: "'%s' is an illegal expression for augmented assignment", _PyPegen_get_expr_name(a) )} + | star_expressions a='=' b='<' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'") } + | star_expressions a='=' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'") } + | star_expressions a='=' b='!' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'") } invalid_ann_assign_target[expr_ty]: | list | tuplewith these changes got these outputs, kind of stuck with our first case @skirpichev could you please share your thoughts on this. Thanks!
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a <> b" File "<string>", line 1 a, b = 1, 2; a <> b ^^ SyntaxError: invalid syntax aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a === b" File "<string>", line 1 a, b = 1, 2; a === b ^^^ SyntaxError: invalid syntax. Maybe you meant '==' instead of '===' aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a =< b" File "<string>", line 1 a, b = 1, 2; a =< b ^^ SyntaxError: invalid syntax. Maybe you meant '<=' instead of '=<' aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a => b" File "<string>", line 1 a, b = 1, 2; a => b ^^ SyntaxError: invalid syntax. Maybe you meant '>=' instead of '=>' aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "a, b = 1, 2; a =! b" File "<string>", line 1 a, b = 1, 2; a =! b ^^ SyntaxError: invalid syntax. Maybe you meant '!=' instead of '=!'skirpichev commented on Jun 12, 2026
Hmm, I'm not sure about the reason. Though:
PS: Please don't post images where you can just show repl output as a plain text.
skirpichev commented on Jun 14, 2026
Currently, you can customize error message for '<>' token by:
Though, see #151464.