]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142349: Fix ast.unparse for lazy import statements (#144893)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Mon, 16 Feb 2026 22:57:49 +0000 (22:57 +0000)
committerGitHub <noreply@github.com>
Mon, 16 Feb 2026 22:57:49 +0000 (22:57 +0000)
The unparser was not handling the `is_lazy` attribute on Import and
ImportFrom AST nodes, causing lazy imports to be unparsed as regular
imports. This broke the round-trip (parse → unparse → reparse) for
any file containing `lazy import` statements.

Lib/_ast_unparse.py

index 1c8741b5a5548334cd95041a07968accde03df72..0c3b1d3a9108a3ad8c71ac99c25d172e10940767 100644 (file)
@@ -239,11 +239,11 @@ class Unparser(NodeVisitor):
             self.traverse(node.value)
 
     def visit_Import(self, node):
-        self.fill("import ")
+        self.fill("lazy import " if node.is_lazy else "import ")
         self.interleave(lambda: self.write(", "), self.traverse, node.names)
 
     def visit_ImportFrom(self, node):
-        self.fill("from ")
+        self.fill("lazy from " if node.is_lazy else "from ")
         self.write("." * (node.level or 0))
         if node.module:
             self.write(node.module)