Skip to content

Replace astor with ast.unparse() and Black #205

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ classifiers = [
]
dependencies = [
"astor",
"black",
"tomli>=1.1.0; python_version < '3.11'"
]

Expand Down
23 changes: 3 additions & 20 deletions src/flynt/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,16 @@
import tokenize
from typing import Optional, Union

import astor
from astor.string_repr import pretty_string
from black import Mode, format_str

from flynt.exceptions import ConversionRefused
from flynt.linting.fstr_lint import FstrInliner
from flynt.utils.format import QuoteTypes, set_quote_type


def nicer_pretty_string(
s,
embedded,
current_line,
uni_lit=False,
):
r = repr(s)
if "\\x" in r:
# If the string contains an escape sequence,
# we need to work around a bug in upstream astor;
# the easiest workaround is to just use the repr
# of the string and be done with it.
return r
return pretty_string(s, embedded, current_line, uni_lit=uni_lit)


def ast_to_string(node: ast.AST) -> str:
# TODO: this could use `ast.unparse` when targeting Python 3.9+ only.
return astor.to_source(node, pretty_string=nicer_pretty_string).rstrip()
# ast.unparse() favors single quotes, use Black to turn them into double quotes
return format_str(ast.unparse(node), mode=Mode()).rstrip()


def is_str_literal(node: ast.AST) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion test/integration/expected_out/multiline_keep.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

[flake8]
max-line-length={length}
inline-quotes="{quotes}\"
inline-quotes="{quotes}"
"""
6 changes: 3 additions & 3 deletions test/test_str_concat/test_candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_find_victims_primitives(pycode_with_2_concats: str):
assert len(ch.victims) == 2

v1, v2 = ch.victims
assert str(v1) == "a + ' World'"
assert str(v1) == 'a + " World"'
assert 'a + " World"' in pycode_with_2_concats.split("\n")[v1.start_line]


Expand All @@ -40,7 +40,7 @@ def test_find_victims_api(pycode_with_2_concats: str, state: State):
assert len(lst) == 2

v1, v2 = lst
assert str(v1) == "a + ' World'"
assert str(v1) == 'a + " World"'
assert 'a + " World"' in pycode_with_2_concats.split("\n")[v1.start_line]


Expand All @@ -53,4 +53,4 @@ def test_find_victims_parens(state: State):
assert len(lst) == 1

v1 = lst[0]
assert str(v1) == """'blah' + (thing - 1)"""
assert str(v1) == '''"blah" + (thing - 1)'''
6 changes: 3 additions & 3 deletions test/test_str_concat/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_string_in_string():
def test_concats_fstring():

txt = """print(f'blah{thing}' + 'blah' + otherThing + f"is {x:d}")"""
expected = """print(f'blah{thing}blah{otherThing}is {x:d}')"""
expected = """print(f"blah{thing}blah{otherThing}is {x:d}")"""

new, changed = transform_concat_from_str(txt)

Expand All @@ -105,7 +105,7 @@ def test_string_in_string_x3():
new, changed = transform_concat_from_str(txt)

assert changed
assert "'blah' +" in new
assert '"blah" +' in new


def test_existing_fstr():
Expand Down Expand Up @@ -133,7 +133,7 @@ def test_existing_fstr_expr():
def test_embedded_fstr():

txt = """print(f"{f'blah{var}' + abc}blah")"""
expected = """print(f'blah{var}{abc}blah')"""
expected = """print(f"blah{var}{abc}blah")"""

new, changed = transform_concat_from_str(txt)

Expand Down
Loading