gh-118082: Improve `import` without names syntax error message (#118083)

This commit is contained in:
Nikita Sobolev 2024-04-23 15:00:52 +03:00 committed by GitHub
parent eb927e9fc8
commit de1f686827
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 219 additions and 149 deletions

View File

@ -1299,10 +1299,14 @@ invalid_group:
invalid_import:
| a='import' ','.dotted_name+ 'from' dotted_name {
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
| 'import' token=NEWLINE {
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
invalid_import_from_targets:
| import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
| token=NEWLINE {
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
invalid_compound_stmt:
| a='elif' named_expression ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'elif' must match an if-statement here") }

View File

@ -1699,6 +1699,18 @@ SyntaxError: Did you mean to use 'from ... import ...' instead?
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> from i import
Traceback (most recent call last):
SyntaxError: Expected one or more names after 'import'
>>> from .. import
Traceback (most recent call last):
SyntaxError: Expected one or more names after 'import'
>>> import
Traceback (most recent call last):
SyntaxError: Expected one or more names after 'import'
>>> (): int
Traceback (most recent call last):
SyntaxError: only single target (not tuple) can be annotated

View File

@ -0,0 +1,3 @@
Improve :exc:`SyntaxError` message for imports without names, like in
``from x import`` and ``import`` cases. It now points
out to users that :keyword:`import` expects at least one name after it.

349
Parser/parser.c generated

File diff suppressed because it is too large Load Diff