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
Confusing SyntaxError
message for x for x if range(1)
#111488
Comments
Thanks for the issue! Will take a bite at this today at night |
This works for me: diff --git a/Grammar/python.gram b/Grammar/python.gram
index ec964d6783..98270fe77f 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -1282,6 +1282,8 @@ invalid_with_item:
RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
invalid_for_target:
+ | 'async'? 'for' star_targets !'in' {
+ RAISE_SYNTAX_ERROR("'in' expected after for-loop variables") }
| 'async'? 'for' a=star_expressions {
RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) } Python 3.13.0a1+ (heads/main-dirty:4a929d432b, Oct 31 2023, 15:32:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> [x for x if range(1)]
File "<stdin>", line 1
[x for x if range(1)]
^^
SyntaxError: 'in' expected after for-loop variables Let me know if this worth a PR. |
This sort of works, but it breaks other error messages, for instance this one:
That used to be:
which is hilariously incorrect. This happens as well with function calls:
now shows:
instead of |
Yep, wrong place, this should be restricted to comprehensions. |
You have the same problem in list comprehensions, is just that there are no tests for that:
this currently shows:
|
@skirpichev If you want a quick PR, you can add tests that cover this case for list/set/dict comprehensions in |
… 'for' in list comprehensions
Bug report
Today I made a typo: instead of
in
I wroteif
.I was quite interested to see the error message:
I don't think that this error message is correct. Why? Because even if we follow the rabbit hole and use
else
, we won't be ever correct:So, maybe we should change it to be something like:
Suggestions about wording are welcome :)
If others agree, I would like to work on this.
Linked PRs
The text was updated successfully, but these errors were encountered: