Skip to content

Commit

Permalink
Move pyupgrade tooling to a dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Jan 6, 2023
1 parent c1a7e96 commit fcaf05c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 0 additions & 18 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions src/darker/syntax_upgrade.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit fcaf05c

Please sign in to comment.