diff --git a/mypy.ini b/mypy.ini index a801264cd..718dd0c62 100644 --- a/mypy.ini +++ b/mypy.ini @@ -83,6 +83,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 c5528ecee..5f47d9cc5 100644 --- a/src/darker/__main__.py +++ b/src/darker/__main__.py @@ -244,24 +244,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)