Skip to content

Commit 7a5f540

Browse files
committed
add helper function, update tests
1 parent 148d8b9 commit 7a5f540

5 files changed

Lines changed: 237 additions & 90 deletions

File tree

Grammar/python.gram

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -801,18 +801,15 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
801801
| in_bitwise_or
802802
| isnot_bitwise_or
803803
| is_bitwise_or
804-
| invalid_diamond_op
805804
| invalid_eqeqeq
806805

807-
invalid_diamond_op:
808-
| a='<' b='>' {
809-
(p->flags & PyPARSE_BARRY_AS_BDFL)
810-
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<>' instead of '< >'?")
811-
: RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'?")
812-
}
813806
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
814807
invalid_eqeqeq:
815-
| a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant 'is' instead of '==='?") }
808+
| a='==' b='=' {
809+
_PyPegen_tokens_are_adjacent(a, b)
810+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant 'is' instead of '==='?")
811+
: NULL
812+
}
816813
noteq_bitwise_or[CmpopExprPair*]:
817814
| (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
818815
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
@@ -1325,9 +1322,21 @@ invalid_assignment:
13251322
"'%s' is an illegal expression for augmented assignment",
13261323
_PyPegen_get_expr_name(a)
13271324
)}
1328-
| star_expressions a='=' b='<' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'?") }
1329-
| star_expressions a='=' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'?") }
1330-
| star_expressions a='=' b='!' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'?") }
1325+
| star_expressions a='=' b='<' {
1326+
_PyPegen_tokens_are_adjacent(a, b)
1327+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'?")
1328+
: NULL
1329+
}
1330+
| star_expressions a='=' b='>' {
1331+
_PyPegen_tokens_are_adjacent(a, b)
1332+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'?")
1333+
: NULL
1334+
}
1335+
| star_expressions a='=' b='!' {
1336+
_PyPegen_tokens_are_adjacent(a, b)
1337+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'?")
1338+
: NULL
1339+
}
13311340
invalid_ann_assign_target[expr_ty]:
13321341
| list
13331342
| tuple

Lib/test/test_syntax.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,17 +3539,22 @@ def test_diamond_operator(self):
35393539
offset=2,
35403540
end_offset=4,
35413541
)
3542-
3543-
def test_diamond_operator_barry_as_flufl(self):
35443542
self._check_error(
3545-
"from __future__ import barry_as_FLUFL\n1 < > 2",
3546-
"Maybe you meant '<>' instead of '< >'",
3547-
lineno=2,
3548-
end_lineno=2,
3549-
offset=3,
3543+
"1 < > 2",
3544+
"invalid syntax",
3545+
lineno=1,
3546+
end_lineno=1,
3547+
offset=5,
35503548
end_offset=6,
35513549
)
35523550

3551+
def test_diamond_operator_barry_as_flufl(self):
3552+
# Under barry_as_FLUFL, '<>' is the valid "not equal" operator
3553+
compile(
3554+
"from __future__ import barry_as_FLUFL\n1<>2",
3555+
"<test>", "exec",
3556+
)
3557+
35533558
def test_triple_equal(self):
35543559
self._check_error(
35553560
"a === b",
@@ -3559,6 +3564,14 @@ def test_triple_equal(self):
35593564
offset=3,
35603565
end_offset=6,
35613566
)
3567+
self._check_error(
3568+
"a == = b",
3569+
"invalid syntax",
3570+
lineno=1,
3571+
end_lineno=1,
3572+
offset=6,
3573+
end_offset=7,
3574+
)
35623575

35633576
def test_eq_lt_typo(self):
35643577
self._check_error(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Improve :exc:`SyntaxError` message for ``===``, ``=<``, ``=>``, ``=!``, and ``<>`` operators, suggesting ``==``, ``<=``, ``>=``, ``!=``, and ``!=`` respectively.
1+
Improve :exc:`SyntaxError` messages for common operator typos coming from other languages: ``=<``, ``=>``, and ``=!`` now suggest ``<=``, ``>=``, and ``!=`` respectively, ``===`` suggests ``is``, and ``<>`` shows a tailored message suggesting ``!=``.

0 commit comments

Comments
 (0)