From 6ca9f1092111cdfc9eec774bdfb781e4359b6406 Mon Sep 17 00:00:00 2001
From: Matthias Bussonnier <bussonniermatthias@gmail.com>
Date: Wed, 12 Aug 2020 20:54:54 -0700
Subject: [PATCH] DRAFT: Function for applying pyupgrade.

Just seeing how it looks like, we would of course need to pass options
in, so i'm thinking only once there is a config file; likely.

So far there's just a function to call `pyupgrade` on given source code.
Applying it to modified lines needs to be implemented, probably best by
refactoring `_reformat_single_file()` so it can be used for different
source-modifying tools.

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 | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/darker/__main__.py b/src/darker/__main__.py
index 7c5711fc3..dfc0ff6a7 100644
--- a/src/darker/__main__.py
+++ b/src/darker/__main__.py
@@ -201,6 +201,24 @@ def _reformat_single_file(  # pylint: disable=too-many-arguments,too-many-locals
     return last_successful_reformat
 
 
+def pyup(content: TextDocument) -> TextDocument:
+    """Upgrade syntax to newer version of Python using `pyupgrade`
+
+    :param content: The Python source code to upgrade
+    :return: The upgraded Python source code
+
+    """
+
+    from pyupgrade._main import _fix_plugins, _fix_tokens, _fix_py36_plus, Settings
+
+    min_version = (3, 6)
+    result = _fix_plugins(content.string, Settings(min_version=min_version))
+    result = _fix_tokens(result, min_version)
+    result = _fix_py36_plus(result, min_version=min_version)
+
+    return TextDocument(result)
+
+
 def modify_file(path: Path, new_content: TextDocument) -> None:
     """Write new content to a file and inform the user by logging"""
     logger.info("Writing %s bytes into %s", len(new_content.string), path)