[issue46073] ast.unparse produces: 'FunctionDef' object has no attribute 'lineno' for valid module
New submission from Brian Carlson : Test file linked. When unparsing the output from ast.parse on a simple class, unparse throws an error: 'FunctionDef' object has no attribute 'lineno' for a valid class and valid AST. It fails when programmatically building the module AST as well. It seems to be from this function: https://github.com/python/cpython/blob/1cbb88736c32ac30fd530371adf53fe7554be0a5/Lib/ast.py#L790 -- components: Library (Lib) files: test.py messages: 408546 nosy: TheRobotCarlson priority: normal severity: normal status: open title: ast.unparse produces: 'FunctionDef' object has no attribute 'lineno' for valid module type: crash versions: Python 3.10, Python 3.11, Python 3.9 Added file: https://bugs.python.org/file50490/test.py ___ Python tracker <https://bugs.python.org/issue46073> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue46073] ast.unparse produces: 'FunctionDef' object has no attribute 'lineno' for valid module
Brian Carlson added the comment: The second solution seems more optimal, in my opinion. I monkey patched the function like this in my own code: ``` def get_type_comment(self, node): comment = self._type_ignores.get(node.lineno) if hasattr(node, "lineno") else node.type_comment if comment is not None: return f" # type: {comment}" ``` Thanks! -- ___ Python tracker <https://bugs.python.org/issue46073> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue46073] ast.unparse produces: 'FunctionDef' object has no attribute 'lineno' for valid module
Brian Carlson added the comment: I don't think passing `lineno` and `column` is preferred. It makes code generation harder because `lineno` and `column` are hard to know ahead of when code is being unparsed. -- ___ Python tracker <https://bugs.python.org/issue46073> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com