From f570629bf36c3548b3487c38042fd4f36e281a21 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Wed, 12 Aug 2020 20:54:54 -0700 Subject: [PATCH] DRAFT: Apply pyupgrade. 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. --- src/darker/__main__.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/darker/__main__.py b/src/darker/__main__.py index e632186f3..6c8b1d4df 100644 --- a/src/darker/__main__.py +++ b/src/darker/__main__.py @@ -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)