Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1416,11 +1416,11 @@ invalid_import:
| 'import' token=NEWLINE {
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
invalid_dotted_as_name:
| dotted_name 'as' !(NAME (',' | ')' | NEWLINE)) 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' !(NAME (',' | ')' | NEWLINE)) 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)) }

Expand Down
4 changes: 1 addition & 3 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Dummy value for Enum and Flag as there are explicit checks for them
# before they have been created.
# This is also why there are checks in EnumType like `if Enum is not None`
Enum = Flag = EJECT = _stdlib_enums = ReprEnum = None
Enum = Flag = EJECT = ReprEnum = None

class nonmember(object):
"""
Expand Down Expand Up @@ -2189,5 +2189,3 @@ def _old_convert_(etype, name, module, filter, source=None, *, boundary=None):
members.sort(key=lambda t: t[0])
cls = etype(name, members, module=module, boundary=boundary or KEEP)
return cls

_stdlib_enums = IntEnum, StrEnum, IntFlag
19 changes: 19 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,25 @@
Traceback (most recent call last):
SyntaxError: cannot use subscript as import target

# Check that we don't raise a "cannot use name as import target" error
# if there is an error in an unrelated statement after ';'

>>> import a as b; None = 1
Traceback (most recent call last):
SyntaxError: cannot assign to None

>>> import a, b as c; d = 1; None = 1
Traceback (most recent call last):
SyntaxError: cannot assign to None

>>> from a import b as c; None = 1
Traceback (most recent call last):
SyntaxError: cannot assign to None

>>> from a import b, c as d; e = 1; None = 1
Traceback (most recent call last):
SyntaxError: cannot assign to None

# Check that we dont raise the "trailing comma" error if there is more
# input to the left of the valid part that we parsed.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :exc:`SyntaxError` message when invalid syntax appears on the same line
as a valid ``import ... as ...`` or ``from ... import ... as ...``
statement. Patch by Brian Schubert.
53 changes: 36 additions & 17 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading