Skip to content

Commit

Permalink
DRAFT: Function for applying pyupgrade.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Carreau authored and akaihola committed Feb 25, 2022
1 parent a6868c1 commit 6ca9f10
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6ca9f10

Please sign in to comment.