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
23 changes: 23 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ 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) }
noteq_bitwise_or[CmpopExprPair*]:
Expand Down Expand Up @@ -1316,6 +1317,21 @@ invalid_assignment:
"'%s' is an illegal expression for augmented assignment",
_PyPegen_get_expr_name(a)
)}
| star_expressions a='=' b='<' {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These only fire for bare statements, so if a => b: still gives the generic error, no? That is probably fine for a first iteration (the = < tokenization makes it look like an assignment), but maybe we should add a test documenting it or a follow-up issue for the if/while case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll open a follow up issue for this, please let me know if you lean towards adding test i fine with that too

_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 Expand Up @@ -1660,3 +1676,10 @@ invalid_bitwise_or:
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?")
: NULL
}

invalid_eqeqeq:
| a='==' b='=' {
_PyPegen_tokens_are_adjacent(a, b)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='?")
: NULL
}
48 changes: 48 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3634,6 +3634,34 @@ def test_double_ampersand(self):
end_offset=6,
)

def test_triple_equal(self):
self._check_error(
"a === b",
r"Maybe you meant '==' 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 +3680,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, and ``===`` suggests ``==``.
Loading
Loading