bpo-43798: Add source location attributes to alias (GH-25324)
* Add source location attributes to alias.
* Move alias star construction to pegen helper.
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
diff --git a/Lib/test/test_asdl_parser.py b/Lib/test/test_asdl_parser.py
index d2c2b51..2c198a6 100644
--- a/Lib/test/test_asdl_parser.py
+++ b/Lib/test/test_asdl_parser.py
@@ -62,7 +62,9 @@ def test_product(self):
alias = self.types['alias']
self.assertEqual(
str(alias),
- 'Product([Field(identifier, name), Field(identifier, asname, opt=True)])')
+ 'Product([Field(identifier, name), Field(identifier, asname, opt=True)], '
+ '[Field(int, lineno), Field(int, col_offset), '
+ 'Field(int, end_lineno, opt=True), Field(int, end_col_offset, opt=True)])')
def test_attributes(self):
stmt = self.types['stmt']
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index bddb3de..6824958 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -335,6 +335,26 @@ def test_non_interned_future_from_ast(self):
mod.body[0].module = " __future__ ".strip()
compile(mod, "<test>", "exec")
+ def test_alias(self):
+ im = ast.parse("from bar import y").body[0]
+ self.assertEqual(len(im.names), 1)
+ alias = im.names[0]
+ self.assertEqual(alias.name, 'y')
+ self.assertIsNone(alias.asname)
+ self.assertEqual(alias.lineno, 1)
+ self.assertEqual(alias.end_lineno, 1)
+ self.assertEqual(alias.col_offset, 16)
+ self.assertEqual(alias.end_col_offset, 17)
+
+ im = ast.parse("from bar import *").body[0]
+ alias = im.names[0]
+ self.assertEqual(alias.name, '*')
+ self.assertIsNone(alias.asname)
+ self.assertEqual(alias.lineno, 1)
+ self.assertEqual(alias.end_lineno, 1)
+ self.assertEqual(alias.col_offset, 16)
+ self.assertEqual(alias.end_col_offset, 17)
+
def test_base_classes(self):
self.assertTrue(issubclass(ast.For, ast.stmt))
self.assertTrue(issubclass(ast.Name, ast.expr))
@@ -1037,7 +1057,8 @@ def test_bad_integer(self):
def test_level_as_none(self):
body = [ast.ImportFrom(module='time',
- names=[ast.alias(name='sleep')],
+ names=[ast.alias(name='sleep',
+ lineno=0, col_offset=0)],
level=None,
lineno=0, col_offset=0)]
mod = ast.Module(body, [])
@@ -1735,6 +1756,7 @@ def test_import_from_multi_line(self):
''').strip()
imp = ast.parse(s).body[0]
self._check_end_pos(imp, 3, 1)
+ self._check_end_pos(imp.names[2], 2, 16)
def test_slices(self):
s1 = 'f()[1, 2] [0]'
@@ -2095,8 +2117,8 @@ def main():
('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 7, 3, 16), 'Exception', ('Load',)), None, [('Pass', (4, 2, 4, 6))])], [], [])], []),
('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [], [], [('Pass', (4, 2, 4, 6))])], []),
('Module', [('Assert', (1, 0, 1, 8), ('Name', (1, 7, 1, 8), 'v', ('Load',)), None)], []),
-('Module', [('Import', (1, 0, 1, 10), [('alias', 'sys', None)])], []),
-('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', 'v', None)], 0)], []),
+('Module', [('Import', (1, 0, 1, 10), [('alias', (1, 7, 1, 10), 'sys', None)])], []),
+('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', (1, 16, 1, 17), 'v', None)], 0)], []),
('Module', [('Global', (1, 0, 1, 8), ['v'])], []),
('Module', [('Expr', (1, 0, 1, 1), ('Constant', (1, 0, 1, 1), 1, None))], []),
('Module', [('Pass', (1, 0, 1, 4))], []),
diff --git a/Lib/test/test_peg_generator/test_c_parser.py b/Lib/test/test_peg_generator/test_c_parser.py
index 5d8f543..b7e80f0 100644
--- a/Lib/test/test_peg_generator/test_c_parser.py
+++ b/Lib/test/test_peg_generator/test_c_parser.py
@@ -314,7 +314,7 @@ def test_same_name_different_types(self) -> None:
)
simple_name[expr_ty]: NAME
import_as_names_from[asdl_alias_seq*]: a[asdl_alias_seq*]=','.import_as_name_from+ { a }
- import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _PyAST_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) }
+ import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _PyAST_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, EXTRA) }
"""
test_source = """
for stmt in ("from a import b as c", "from . import a as b"):