Skip to content

Commit d89dfcf

Browse files
committed
add helper function, update tests
1 parent d176b08 commit d89dfcf

5 files changed

Lines changed: 383 additions & 423 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) }
@@ -1323,9 +1320,21 @@ invalid_assignment:
13231320
"'%s' is an illegal expression for augmented assignment",
13241321
_PyPegen_get_expr_name(a)
13251322
)}
1326-
| star_expressions a='=' b='<' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'?") }
1327-
| star_expressions a='=' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'?") }
1328-
| star_expressions a='=' b='!' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'?") }
1323+
| star_expressions a='=' b='<' {
1324+
_PyPegen_tokens_are_adjacent(a, b)
1325+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'?")
1326+
: NULL
1327+
}
1328+
| star_expressions a='=' b='>' {
1329+
_PyPegen_tokens_are_adjacent(a, b)
1330+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'?")
1331+
: NULL
1332+
}
1333+
| star_expressions a='=' b='!' {
1334+
_PyPegen_tokens_are_adjacent(a, b)
1335+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'?")
1336+
: NULL
1337+
}
13291338
invalid_ann_assign_target[expr_ty]:
13301339
| list
13311340
| tuple

Lib/test/test_syntax.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,17 +3514,22 @@ def test_diamond_operator(self):
35143514
offset=2,
35153515
end_offset=4,
35163516
)
3517-
3518-
def test_diamond_operator_barry_as_flufl(self):
35193517
self._check_error(
3520-
"from __future__ import barry_as_FLUFL\n1 < > 2",
3521-
"Maybe you meant '<>' instead of '< >'",
3522-
lineno=2,
3523-
end_lineno=2,
3524-
offset=3,
3518+
"1 < > 2",
3519+
"invalid syntax",
3520+
lineno=1,
3521+
end_lineno=1,
3522+
offset=5,
35253523
end_offset=6,
35263524
)
35273525

3526+
def test_diamond_operator_barry_as_flufl(self):
3527+
# Under barry_as_FLUFL, '<>' is the valid "not equal" operator
3528+
compile(
3529+
"from __future__ import barry_as_FLUFL\n1<>2",
3530+
"<test>", "exec",
3531+
)
3532+
35283533
def test_triple_equal(self):
35293534
self._check_error(
35303535
"a === b",
@@ -3534,6 +3539,14 @@ def test_triple_equal(self):
35343539
offset=3,
35353540
end_offset=6,
35363541
)
3542+
self._check_error(
3543+
"a == = b",
3544+
"invalid syntax",
3545+
lineno=1,
3546+
end_lineno=1,
3547+
offset=6,
3548+
end_offset=7,
3549+
)
35373550

35383551
def test_eq_lt_typo(self):
35393552
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)