gh-123539: Improve SyntaxError msg for `import as` with not a name (#… · python/cpython@a6ddd07 · GitHub
Skip to content

Commit a6ddd07

Browse files
authored
gh-123539: Improve SyntaxError msg for import as with not a name (#123629)
1 parent 39afd29 commit a6ddd07

4 files changed

Lines changed: 1058 additions & 731 deletions

File tree

Grammar/python.gram

Lines changed: 16 additions & 6 deletions

Lib/test/test_syntax.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,56 @@
19811981
Traceback (most recent call last):
19821982
SyntaxError: cannot assign to __debug__
19831983
1984+
>>> import a as b.c
1985+
Traceback (most recent call last):
1986+
SyntaxError: cannot use attribute as import target
1987+
1988+
>>> import a.b as (a, b)
1989+
Traceback (most recent call last):
1990+
SyntaxError: cannot use tuple as import target
1991+
1992+
>>> import a, a.b as 1
1993+
Traceback (most recent call last):
1994+
SyntaxError: cannot use literal as import target
1995+
1996+
>>> import a.b as 'a', a
1997+
Traceback (most recent call last):
1998+
SyntaxError: cannot use literal as import target
1999+
2000+
>>> from a import (b as c.d)
2001+
Traceback (most recent call last):
2002+
SyntaxError: cannot use attribute as import target
2003+
2004+
>>> from a import b as 1
2005+
Traceback (most recent call last):
2006+
SyntaxError: cannot use literal as import target
2007+
2008+
>>> from a import (
2009+
... b as f())
2010+
Traceback (most recent call last):
2011+
SyntaxError: cannot use function call as import target
2012+
2013+
>>> from a import (
2014+
... b as [],
2015+
... )
2016+
Traceback (most recent call last):
2017+
SyntaxError: cannot use list as import target
2018+
2019+
>>> from a import (
2020+
... b,
2021+
... c as ()
2022+
... )
2023+
Traceback (most recent call last):
2024+
SyntaxError: cannot use tuple as import target
2025+
2026+
>>> from a import b, с as d[e]
2027+
Traceback (most recent call last):
2028+
SyntaxError: cannot use subscript as import target
2029+
2030+
>>> from a import с as d[e], b
2031+
Traceback (most recent call last):
2032+
SyntaxError: cannot use subscript as import target
2033+
19842034
# Check that we dont raise the "trailing comma" error if there is more
19852035
# input to the left of the valid part that we parsed.
19862036
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)