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

Fix compatibility with Flynt v1.0.0 #512

Merged
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
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pygments.lexers =

[options.extras_require]
flynt =
flynt>=0.76,<0.78
flynt>=0.76
isort =
isort>=5.0.1
color =
Expand All @@ -58,7 +58,7 @@ test =
black>=21.7b1 # to prevent Mypy error about `gen_python_files`, see issue #189
cryptography>=3.3.2 # through twine, fixes CVE-2020-36242
defusedxml>=0.7.1
flynt>=0.76,<0.78
flynt>=0.76
isort>=5.0.1
mypy>=0.990
pip-requirements-parser
Expand Down
30 changes: 18 additions & 12 deletions src/darker/fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@
from darker.exceptions import MissingPackageError
from darker.git import EditedLinenumsDiffer
from darker.utils import TextDocument
from flynt.pyproject_finder import find_pyproject_toml, parse_pyproject_toml

try:
import flynt

flynt_fstringify_code_by_line = flynt.process.fstringify_code_by_line
if hasattr(flynt.state, "State"):
State = flynt.state.State
flynt_version = tuple(map(int, flynt.__version__.split(".")))
if flynt_version >= (0, 78):
from flynt.state import State
else:
State = None
if flynt_version < (1, 0, 0):
from flynt.process import fstringify_code_by_line
from flynt.pyproject_finder import find_pyproject_toml, parse_pyproject_toml
else:
from flynt.code_editor import fstringify_code_by_line
from flynt.utils.pyproject_finder import (
find_pyproject_toml,
parse_pyproject_toml,
)
except ImportError:
# `flynt` is an optional dependency. Prevent the `ImportError` if it's missing.
flynt = None
State = None

def flynt_fstringify_code_by_line( # type: ignore[misc]
*args: Any, **kwargs: Any
) -> str:
def fstringify_code_by_line(*args: Any, **kwargs: Any) -> str: # type: ignore[misc]
"""Fake `flynt.fstringify_code_by_line()` to use when `flynt` isn't installed"""
raise MissingPackageError(
"No module named 'flynt'. Please install the 'flynt' package before using"
Expand Down Expand Up @@ -66,7 +72,7 @@ def apply_flynt(
return _call_flynt_fstringify(content, state)


def _get_flynt_configuration(src: Path) -> Optional[State]:
def _get_flynt_configuration(src: Path) -> Optional[State]: # type: ignore[no-any-unimported]
"""Read ``pyproject.toml`` Flynt configuration for the given Python file

:param src: The absolute path to the Python file to run Flynt on. This must be the
Expand Down Expand Up @@ -102,19 +108,19 @@ def _get_flynt_configuration(src: Path) -> Optional[State]:
return state


def _call_flynt_fstringify(
def _call_flynt_fstringify( # type: ignore[no-any-unimported]
content: TextDocument, state: Optional[State]
) -> TextDocument:
"""Call ``flynt.process.fstringify_code_by_line()``, return result `TextDocument`
"""Call ``flynt.code_editor.fstringify_code_by_line()``, return result `TextDocument`

:param content: The contents of the Python source code file to fstringify
:param state: The ``flynt`` configuration to use, or ``None`` for ``flynt<0.78``
:return: Original Python source code contents with modifications from ``flynt``

"""
logger.debug("flynt.process.fstringify_code_by_line(code=...)")
logger.debug("flynt.code_editor.fstringify_code_by_line(code=...)")
args = () if state is None else (state,) # `()` for flynt<0.78, (state,) for >=0.78
result, _ = flynt_fstringify_code_by_line(content.string, *args)
result, _ = fstringify_code_by_line(content.string, *args)
return TextDocument.from_str(
result,
encoding=content.encoding,
Expand Down
5 changes: 3 additions & 2 deletions src/darker/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def flynt_present(present: bool) -> Generator[None, None, None]:
with _package_present("flynt", present) as fake_flynt_module:
if present:
# dummy module and function required by `fstring`:
fake_flynt_module.process = ModuleType("process") # type: ignore
fake_flynt_module.process.fstringify_code_by_line = None # type: ignore
fake_flynt_module.__version__ = "1.0.0" # type: ignore
fake_flynt_module.code_editor = ModuleType("process") # type: ignore
fake_flynt_module.code_editor.fstringify_code_by_line = None # type: ignore
yield