-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-151303 : Improve SyntaxError suggestions for common operator typos and cross-language mistakes
#151375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-151303 : Improve SyntaxError suggestions for common operator typos and cross-language mistakes
#151375
Changes from all commits
9472172
65cf172
34a7ef1
431174b
bb79f1f
30e8188
f039f43
b2c9971
d176b08
d89dfcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3505,6 +3505,79 @@ def test_ifexp_body_stmt_else_stmt(self): | |
| ]: | ||
| self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg) | ||
|
|
||
| def test_diamond_operator(self): | ||
| self._check_error( | ||
| "1<>2", | ||
| r'Are you trying to overthrow the SC\? Use operator "!="!', | ||
| lineno=1, | ||
| end_lineno=1, | ||
| offset=2, | ||
| end_offset=4, | ||
| ) | ||
| self._check_error( | ||
| "1 < > 2", | ||
| "invalid syntax", | ||
| lineno=1, | ||
| end_lineno=1, | ||
| offset=5, | ||
| end_offset=6, | ||
| ) | ||
|
|
||
| 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 on lines
+3528
to
+3531
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check error for 'a < > b' in this case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant, please add a test.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh 😿 my bad, i'll add 👍 |
||
|
|
||
| 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_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.""" | ||
|
|
||
| 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 ``!=``. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -931,6 +931,10 @@ _PyPegen_check_barry_as_flufl(Parser *p, Token* t) { | |
| return -1; | ||
| } | ||
| if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) { | ||
| if (strcmp(tok_str, "<>") == 0) { | ||
| RAISE_SYNTAX_ERROR("invalid syntax. Are you trying to overthrow the SC? Use operator \"!=\"!"); | ||
| return -1; | ||
| } | ||
|
Comment on lines
+934
to
+937
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer not use this workaround for #151464. Leave "<>" case for another PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should i revert the changes for this case ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my suggestion, yes. |
||
| return strcmp(tok_str, "!="); | ||
| } | ||
| return 0; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.