berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
790 stars 102 forks source link

astor.to_source pretty_string #224

Open carbonium14 opened 2 months ago

carbonium14 commented 2 months ago

When I used astor.to_source, I found that during the conversion process, when a line of code is very long, it will automatically wrap.

# astor: 0.8.1
node = ast.parse('this_is_a_sentence_that_is_very_long=this_is_a_sentence_that_is_extremely_long')
code = astor.to_source(node=node)
print(code)
# the result is:
# this_is_a_sentence_that_is_very_long = (
#     this_is_a_sentence_that_is_extremely_long)

What I hope to get is this_is_a_sentence_that_is_very_long=this_is_a_sentence_that_is_extremely_long, that is, no matter how long a line of code is, it will be gathered into one line.I found that the pretty_string function can manually format the code, but I found that no matter how I set this function, I cannot achieve the expected output. How can I solve this problem?

berkerpeksag commented 2 months ago

Thank you for the report! Could you please try the following snippet?

>>> import ast, astor
>>> node = ast.parse('this_is_a_sentence_that_is_very_long=this_is_a_sentence_that_is_extremely_long')
>>> code = astor.to_source(node=node, pretty_source=lambda source: ''.join(source))
>>> print(code)
this_is_a_sentence_that_is_very_long = this_is_a_sentence_that_is_extremely_long
carbonium14 commented 2 months ago

Thank you for the report! Could you please try the following snippet?

>>> import ast, astor
>>> node = ast.parse('this_is_a_sentence_that_is_very_long=this_is_a_sentence_that_is_extremely_long')
>>> code = astor.to_source(node=node, pretty_source=lambda source: ''.join(source))
>>> print(code)
this_is_a_sentence_that_is_very_long = this_is_a_sentence_that_is_extremely_long

it works! thanks for your answer!

berkerpeksag commented 2 months ago

Awesome! I'd be happy to review a documentation PR that mentions how to disable PEP 8 styling.