Skip to content

Commit b2c9971

Browse files
committed
Improve SyntaxError suggestions for common operator typos
1 parent f039f43 commit b2c9971

4 files changed

Lines changed: 11 additions & 7 deletions

File tree

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ invalid_diamond_op:
812812
}
813813
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
814814
invalid_eqeqeq:
815-
| a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='?") }
815+
| a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant 'is' instead of '==='?") }
816816
noteq_bitwise_or[CmpopExprPair*]:
817817
| (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
818818
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }

Lib/test/test_syntax.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,12 +3507,12 @@ def test_ifexp_body_stmt_else_stmt(self):
35073507

35083508
def test_diamond_operator(self):
35093509
self._check_error(
3510-
"1 < > 2",
3511-
"Maybe you meant '!=' instead of '<>'?",
3510+
"1<>2",
3511+
r'Are you trying to overthrow the SC\? Use operator "!="!',
35123512
lineno=1,
35133513
end_lineno=1,
3514-
offset=3,
3515-
end_offset=6,
3514+
offset=2,
3515+
end_offset=4,
35163516
)
35173517

35183518
def test_diamond_operator_barry_as_flufl(self):
@@ -3528,7 +3528,7 @@ def test_diamond_operator_barry_as_flufl(self):
35283528
def test_triple_equal(self):
35293529
self._check_error(
35303530
"a === b",
3531-
"Maybe you meant '==' instead of '==='?",
3531+
"Maybe you meant 'is' instead of '==='?",
35323532
lineno=1,
35333533
end_lineno=1,
35343534
offset=3,

Parser/action_helpers.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ _PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
931931
return -1;
932932
}
933933
if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) {
934+
if (strcmp(tok_str, "<>") == 0) {
935+
RAISE_SYNTAX_ERROR("Are you trying to overthrow the SC? Use operator \"!=\"!");
936+
return -1;
937+
}
934938
return strcmp(tok_str, "!=");
935939
}
936940
return 0;

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)