Skip to content

Commit

Permalink
DRAFT: Apply pyupgrade.
Browse files Browse the repository at this point in the history
Just seeing how it looks like, we woudl of course need to pass options
in, so i'm thinking only once there is a config file; likely.

This is likely incorrect as the edited lines could likely be off once
black has been ran as it can rewrap stuff.

It also does use pyupgrade private internal API, and the author has
explicitly stated they they were not willing to have public API beyond
the CLI.

We should also likely run this _before_ black; maybe even before isort ?
not sure if it might remove imports... But at least it does not pass the
ast unchange part.
  • Loading branch information
Carreau committed Aug 13, 2020
1 parent 8e57464 commit f570629
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,42 @@ def format_edited_parts(
# 10. A re-formatted Python file which produces an identical AST was
# created successfully - write an updated file or print the diff
# if there were any changes to the original
result_str = pyup(result_str, edited_lines, edited_linenums)

if result_str != worktree_content:
# `result_str` is just `chosen_lines` concatenated with newlines.
# We need both forms when showing diffs or modifying files.
# Pass them both on to avoid back-and-forth conversion.
yield src, worktree_content, result_str, chosen_lines
yield src, worktree_content, result_str, result_str.splitlines()
break


def pyup(content, edited_lines, edited_linenums):

from pyupgrade import (
_fix_py2_compatible,
_fix_tokens,
_fix_percent_format,
_fix_py3_plus,
_fix_py36_plus,
)

min_version = (3, 6)
keep_mock = True
content = _fix_py2_compatible(content)
content = _fix_tokens(content, min_version)
content = _fix_percent_format(content)
content = _fix_py3_plus(content, min_version, keep_mock)
content = _fix_py36_plus(content)
content = content.splitlines()

pyup_opcodes = diff_and_get_opcodes(edited_lines, content)
pyupgrade_chunks = list(opcodes_to_chunks(pyup_opcodes, edited_lines, content))
pyup_chosen_lines: List[str] = list(choose_lines(pyupgrade_chunks, edited_linenums))
pyup_result_str = joinlines(pyup_chosen_lines)
return pyup_result_str


def modify_file(path: Path, new_content: str) -> None:
"""Write new content to a file and inform the user by logging"""
logger.info("Writing %s bytes into %s", len(new_content), path)
Expand Down

0 comments on commit f570629

Please sign in to comment.