Skip to content

Commit e777866

Browse files
Make error for invalid lazy from __future__ ... import prettier
1 parent 2d3681e commit e777866

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ import_name[stmt_ty]:
230230
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
231231
import_from[stmt_ty]:
232232
| lazy="lazy"? 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
233-
_PyPegen_checked_future_import(p, b->v.Name.id, c, _PyPegen_seq_count_dots(a), lazy ? 1 : 0, EXTRA) }
233+
_PyPegen_checked_future_import(p, b->v.Name.id, c, _PyPegen_seq_count_dots(a), lazy, EXTRA) }
234234
| lazy="lazy"? 'from' a=('.' | '...')+ 'import' b=import_from_targets {
235235
_PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), lazy ? 1 : 0, EXTRA) }
236236
import_from_targets[asdl_alias_seq*]:

Lib/test/test_import/test_lazy_imports.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@ def test_lazy_try_except_from_star(self):
219219

220220
def test_lazy_future_import(self):
221221
"""lazy from __future__ import should raise SyntaxError."""
222-
with self.assertRaises(SyntaxError):
222+
with self.assertRaises(SyntaxError) as cm:
223223
import test.test_import.data.lazy_imports.lazy_future_import
224+
# Check we highlight 'lazy' (column offset 0, end offset 4)
225+
self.assertEqual(cm.exception.offset, 1)
226+
self.assertEqual(cm.exception.end_offset, 5)
224227

225228
def test_lazy_import_func(self):
226229
"""lazy import inside function should raise SyntaxError."""

Parser/action_helpers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,11 +1943,11 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
19431943

19441944
stmt_ty
19451945
_PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq * names, int level,
1946-
int is_lazy, int lineno, int col_offset, int end_lineno, int end_col_offset,
1947-
PyArena *arena) {
1946+
expr_ty lazy_token, int lineno, int col_offset, int end_lineno, int end_col_offset,
1947+
PyArena *arena) {
19481948
if (level == 0 && PyUnicode_CompareWithASCIIString(module, "__future__") == 0) {
1949-
if (is_lazy) {
1950-
RAISE_SYNTAX_ERROR("lazy from __future__ import is not allowed");
1949+
if (lazy_token) {
1950+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(lazy_token, "lazy from __future__ import is not allowed");
19511951
return NULL;
19521952
}
19531953
for (Py_ssize_t i = 0; i < asdl_seq_LEN(names); i++) {
@@ -1957,7 +1957,7 @@ _PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq * na
19571957
}
19581958
}
19591959
}
1960-
return _PyAST_ImportFrom(module, names, level, is_lazy, lineno, col_offset, end_lineno, end_col_offset, arena);
1960+
return _PyAST_ImportFrom(module, names, level, lazy_token ? 1 : 0, lineno, col_offset, end_lineno, end_col_offset, arena);
19611961
}
19621962

19631963
asdl_stmt_seq*

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Parser/pegen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
366366
expr_ty _PyPegen_get_last_comprehension_item(comprehension_ty comprehension);
367367
void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions);
368368
stmt_ty _PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq *,
369-
int , int, int , int , int , int, PyArena *);
369+
int, expr_ty, int, int, int, int, PyArena *);
370370
asdl_stmt_seq* _PyPegen_register_stmts(Parser *p, asdl_stmt_seq* stmts);
371371
stmt_ty _PyPegen_register_stmt(Parser *p, stmt_ty s);
372372

0 commit comments

Comments
 (0)