Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retire astor, as ast.unparse exists, and migrate it into API-compatible shims of ast for older versions of python #204

Open
KOLANICH opened this issue Nov 19, 2021 · 6 comments

Comments

@KOLANICH
Copy link
Contributor

No description provided.

@SamuelMarks
Copy link

@Kodiologist
Copy link
Contributor

Hy also does this, since hylang/hy#1999. astor might as well do it internally.

@leycec
Copy link

leycec commented Jul 8, 2023

Necrobump. The astor.code_to_ast() function decompiling arbitrary code objects into corresponding ASTs still appears to have no equivalent in any other library – including the standard ast module. Is that correct? If so, astor should absolutely not retire but instead mono-focus on this one thing astor still does that nobody else does.

In fact, that's exactly how I discovered astor: via this StackOverflow question, whose first comment namedrops astor as the only available solution. Long live astor! ✊

@akaihola
Copy link

akaihola commented Feb 7, 2025

Annoyingly, ast.unparse() outputs single quoted strings by default while modern code reformatters default to double quotes.

ast.unparse() behavior is impossible to change since it:

  • doesn't provide any options to tweak its behavior
  • uses the ast._Unparser class which is not part of the public API

ikamensh/flynt#195 is currently struggling with this and would benefit from astor continuing to be maintained and made compatible with Python 3.14.

@Kodiologist
Copy link
Contributor

modern code reformatters default to double quotes

That seems like an unforced error. If I recall correctly, Python's repr has always used single quotes, and single quotes have always predominated in official documentation. I'm not sure why Black decided to go with double quotes.

@akaihola
Copy link

Another difference in behavior between astor and ast.unparse():

>>> import ast
>>> import astor
>>> node = ast.JoinedStr(
...     values=[ast.FormattedValue(value=ast.IfExp(test=ast.Name(id='c'), body=ast.Name(id='a'), orelse=ast.Name(id='b')), conversion=-1)]
... )
>>> print(ast.unparse(node))
f'{(a if c else b)}'
>>> print(astor.to_source(node))
f"""{a if c else b}"""

This is being discussed in ikamensh/flynt#205, preventing migrating from astor to ast.unparse().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants