https://github.com/python/cpython/commit/18c04f2e2a7e47329e9eefc5f269afe2d68729b0
commit: 18c04f2e2a7e47329e9eefc5f269afe2d68729b0
branch: main
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-02-16T22:57:49Z
summary:
gh-142349: Fix ast.unparse for lazy import statements (#144893)
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.
files:
M Lib/_ast_unparse.py
diff --git a/Lib/_ast_unparse.py b/Lib/_ast_unparse.py
index 1c8741b5a55483..0c3b1d3a9108a3 100644
--- a/Lib/_ast_unparse.py
+++ b/Lib/_ast_unparse.py
@@ -239,11 +239,11 @@ def visit_NamedExpr(self, node):
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)
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]