Skip to content

Commit ce3e201

Browse files
committed
gh-150459: Fix SyntaxError message for from x lazy import y
1 parent 7a468a1 commit ce3e201

4 files changed

Lines changed: 480 additions & 388 deletions

File tree

Grammar/python.gram

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ import_name[stmt_ty]:
229229

230230
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
231231
import_from[stmt_ty]:
232+
| invalid_import_from
232233
| lazy="lazy"? 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
233234
_PyPegen_checked_future_import(p, b->v.Name.id, c, _PyPegen_seq_count_dots(a), lazy, EXTRA) }
234235
| lazy="lazy"? 'from' a=('.' | '...')+ 'import' b=import_from_targets {
@@ -1443,6 +1444,11 @@ invalid_import_from_as_name:
14431444
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14441445
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
14451446

1447+
invalid_import_from:
1448+
| 'from' ('.' | '...')* dotted_name a="lazy" 'import' import_from_targets {
1449+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
1450+
"use 'lazy from ... ' instead of 'from ... lazy import'") }
1451+
14461452
invalid_import_from_targets:
14471453
| import_from_as_names ',' NEWLINE {
14481454
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

Lib/test/test_syntax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,10 @@ def inner():
36183618
lazy from collections import deque
36193619
""", "lazy from ... import not allowed inside functions")
36203620

3621+
self._check_error("""\
3622+
from os lazy import path
3623+
""", "use 'lazy from ... ' instead of 'from ... lazy import'")
3624+
36213625
def test_lazy_import_valid_cases(self):
36223626
"""Test that lazy imports work at module level."""
36233627
# These should compile without errors
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :exc:`SyntaxError` error message for ``from x lazy import y``.

0 commit comments

Comments
 (0)