-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-123539: Improve SyntaxError msg for import as
with not a name
#123629
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to not change or introduce new first-pass rules when introducing error messages. If you have problems with precedence, perhaps you can just move the invalid rule the first one
When you're done making the requested changes, leave the comment: |
Well, I think in this case it's not possible to do it otherwise. You wanna to be checking the FWIW my guesstimate is that this might be frequent enough that changing existing rules is warranted. |
Yeah, but we are adding now a lookahead ( |
I will try some other approaches, I might come up with something simplier. In the meantime I also noticed that >>> from a import b.c
File "<stdin>", line 1
from a import b.c
^
SyntaxError: invalid syntax this can also be improved, not only |
Whatever you prefer, it's ok to do it all here as we are already discussing this rule :) |
I've spent several hours trying something else:
Right now I think that it is better to close this PR, unfortunatelly. |
If @lysnikolaou it's not too worried about the lookahead I am happy to consider landing it as is currently, but I am a bit worried that this also makes the grammar more difficult to reason about in the official docs because there it won't be apparent why the lookahead is needed (and other rules don't have it). |
This works: diff --git a/Grammar/python.gram b/Grammar/python.gram
index c8415552b28..8eb9bb7473c 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -218,17 +218,17 @@ import_from_targets[asdl_alias_seq*]:
import_from_as_names[asdl_alias_seq*]:
| a[asdl_alias_seq*]=','.import_from_as_name+ { a }
import_from_as_name[alias_ty]:
- | a=NAME 'as' b=NAME &(',' | ')' | NEWLINE) {
- _PyAST_alias(a->v.Name.id, b->v.Name.id, EXTRA) }
| invalid_import_from_as_name
- | a=NAME { _PyAST_alias(a->v.Name.id, NULL, EXTRA) }
+ | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
+ (b) ? ((expr_ty) b)->v.Name.id : NULL,
+ EXTRA) }
dotted_as_names[asdl_alias_seq*]:
| a[asdl_alias_seq*]=','.dotted_as_name+ { a }
dotted_as_name[alias_ty]:
- | a=dotted_name 'as' b=NAME &(',' | NEWLINE) {
- _PyAST_alias(a->v.Name.id, b->v.Name.id, EXTRA) }
| invalid_dotted_as_name
- | a=dotted_name { _PyAST_alias(a->v.Name.id, NULL, EXTRA) }
+ | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
+ (b) ? ((expr_ty) b)->v.Name.id : NULL,
+ EXTRA) }
dotted_name[expr_ty]:
| a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
| NAME
@@ -1324,11 +1324,11 @@ invalid_import_from_targets:
| token=NEWLINE {
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
invalid_dotted_as_name:
- | dotted_name 'as' a=expression {
+ | dotted_name 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
invalid_import_from_as_name:
- | NAME 'as' a=expression {
+ | NAME 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
Notice this doesn't modify the current working rules but just adds the invalid with higher precedence + filtering of the expected one. The filtering is needed otherwise it will match something valid if there is a syntax error afterwards. |
@pablogsal thanks a lot for your help, great idea! Sorry, it took me so long to follow up on this PR. Now it should be ready to review again 🫶 |
@pablogsal @lysnikolaou I would like to get this merged before the feature freeze, it would be amazing if you have some time to look at it :) ![]() |
I'm merging this for now but could you create another PR adding the improvement to the what's new for 3.14 in the syntax error section? |
Thanks a lot, @pablogsal! Without your help - it would be so much harder! 🫶 Sure, will do! |
Fantastic job @sobolevn 👌🤘 |
Example output:

SyntaxError
message forimport a as b.c
#123539