Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,15 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
| in_bitwise_or
| isnot_bitwise_or
| is_bitwise_or
| invalid_eqeqeq

eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
invalid_eqeqeq:
| a='==' b='=' {
_PyPegen_tokens_are_adjacent(a, b)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant 'is' instead of '==='?")
: NULL
}
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) }
Expand Down Expand Up @@ -1316,6 +1323,21 @@ invalid_assignment:
"'%s' is an illegal expression for augmented assignment",
_PyPegen_get_expr_name(a)
)}
| star_expressions a='=' b='<' {
_PyPegen_tokens_are_adjacent(a, b)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<=' instead of '=<'?")
: NULL
}
| star_expressions a='=' b='>' {
_PyPegen_tokens_are_adjacent(a, b)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '>=' instead of '=>'?")
: NULL
}
| star_expressions a='=' b='!' {
_PyPegen_tokens_are_adjacent(a, b)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '=!'?")
: NULL
}
invalid_ann_assign_target[expr_ty]:
| list
| tuple
Expand Down
72 changes: 72 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3631,9 +3631,61 @@ def test_double_ampersand(self):
lineno=2,
end_lineno=2,
offset=5,
)

def test_diamond_operator(self):
self._check_error(
"1<>2",
"invalid syntax",
lineno=1,
end_lineno=1,
offset=2,
end_offset=4,
)

def test_diamond_operator_barry_as_flufl(self):
# Under barry_as_FLUFL, '<>' is the valid "not equal" operator
compile(
"from __future__ import barry_as_FLUFL\n1<>2",
"<test>", "exec",
)
Comment thread
skirpichev marked this conversation as resolved.
self._check_error(
"from __future__ import barry_as_FLUFL\na < > b",
"invalid syntax",
lineno=2,
end_lineno=2,
offset=5,
end_offset=6,
)

def test_triple_equal(self):
self._check_error(
"a === b",
r"Maybe you meant 'is' instead of '==='\?",
lineno=1,
end_lineno=1,
offset=3,
end_offset=6,
)
self._check_error(
"a == = b",
"invalid syntax",
lineno=1,
end_lineno=1,
offset=6,
end_offset=7,
)

def test_eq_lt_typo(self):
self._check_error(
"a =< b",
r"Maybe you meant '<=' instead of '=<'\?",
lineno=1,
end_lineno=1,
offset=3,
end_offset=5,
)

def test_double_pipe(self):
self._check_error(
"a || b",
Expand All @@ -3652,6 +3704,26 @@ def test_double_pipe(self):
end_offset=6,
)

def test_eq_gt_typo(self):
self._check_error(
"a => b",
r"Maybe you meant '>=' instead of '=>'\?",
lineno=1,
end_lineno=1,
offset=3,
end_offset=5,
)

def test_eq_bang_typo(self):
self._check_error(
"a =! b",
r"Maybe you meant '!=' instead of '=!'\?",
lineno=1,
end_lineno=1,
offset=3,
end_offset=5,
)


class LazyImportRestrictionTestCase(SyntaxErrorTestCase):
"""Test syntax restrictions for lazy imports."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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 ``!=``.
Loading
Loading