From bf3656faf885b2c63f2504a8e8dbad1da00db8d4 Mon Sep 17 00:00:00 2001
From: Antti Kaihola <13725+akaihola@users.noreply.github.com>
Date: Mon, 14 Nov 2022 20:47:10 +0200
Subject: [PATCH] Move `pyupgrade` tooling to a dedicated module

---
 mypy.ini                     |  3 +++
 src/darker/__main__.py       | 18 ------------------
 src/darker/syntax_upgrade.py | 29 +++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 18 deletions(-)
 create mode 100644 src/darker/syntax_upgrade.py

diff --git a/mypy.ini b/mypy.ini
index 57bbb29bd..b64b4e9f2 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -87,6 +87,9 @@ ignore_missing_imports = True
 [mypy-pytest.*]
 ignore_missing_imports = True
 
+[mypy-pyupgrade.*]
+ignore_missing_imports = True
+
 [mypy-setuptools.*]
 ignore_missing_imports = True
 
diff --git a/src/darker/__main__.py b/src/darker/__main__.py
index fca92f660..206f0e681 100644
--- a/src/darker/__main__.py
+++ b/src/darker/__main__.py
@@ -260,24 +260,6 @@ def _blacken_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)
diff --git a/src/darker/syntax_upgrade.py b/src/darker/syntax_upgrade.py
new file mode 100644
index 000000000..62e541fd5
--- /dev/null
+++ b/src/darker/syntax_upgrade.py
@@ -0,0 +1,29 @@
+"""Wrapper for applying `pyupgrade` on Python source code"""
+
+from darker.utils import TextDocument
+
+try:
+    from pyupgrade import _main as pyupgrade_main
+except ImportError:
+    # `pyupgrade` is an optional dependency. Prevent the `ImportError` if it's missing.
+    pyupgrade_main = None
+
+
+__all__ = ["apply_pyupgrade", "pyupgrade_main"]
+
+
+def apply_pyupgrade(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
+
+    """
+    # pylint: disable=protected-access
+    min_version = (3, 6)
+    result = pyupgrade_main._fix_plugins(
+        content.string, pyupgrade_main.Settings(min_version=min_version)
+    )
+    result = pyupgrade_main._fix_tokens(result, min_version)
+    result = pyupgrade_main._fix_py36_plus(result, min_version=min_version)
+    return TextDocument.from_str(result)