Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Apply pyupgrade. #51

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ isort =
isort>=5.0.1
color =
Pygments>=2.4.0
pyupgrade =
pyupgrade>=2.31.0
test =
# NOTE: remember to keep `constraints-oldest.txt` in sync with these
airium>=0.2.3
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)