Skip to content

Commit

Permalink
[3.11] gh-92986: Fix ast.unparse when ImportFrom.level is None (GH-92992
Browse files Browse the repository at this point in the history
) (GH-96593)

This doesn't happen naturally, but is allowed by the ASDL and compiler.
We don't want to change ASDL for backward compatibility reasons
(GH-57645, GH-92987)
(cherry picked from commit 200c9a8)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
  • Loading branch information
isidentical and hauntsaninja authored Sep 6, 2022
1 parent 08d8058 commit a0848d1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ def visit_Import(self, node):

def visit_ImportFrom(self, node):
self.fill("from ")
self.write("." * node.level)
self.write("." * (node.level or 0))
if node.module:
self.write(node.module)
self.write(" import ")
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ def test_invalid_fstring_backslash(self):
def test_invalid_yield_from(self):
self.check_invalid(ast.YieldFrom(value=None))

def test_import_from_level_none(self):
tree = ast.ImportFrom(module='mod', names=[ast.alias(name='x')])
self.assertEqual(ast.unparse(tree), "from mod import x")
tree = ast.ImportFrom(module='mod', names=[ast.alias(name='x')], level=None)
self.assertEqual(ast.unparse(tree), "from mod import x")

def test_docstrings(self):
docstrings = (
'this ends with double quote"',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`ast.unparse` when ``ImportFrom.level`` is None

0 comments on commit a0848d1

Please sign in to comment.