Skip to content

Commit

Permalink
Merge pull request #512 from baod-rate/flynt-1.0.0-compatibility
Browse files Browse the repository at this point in the history
Fix compatibility with Flynt v1.0.0
  • Loading branch information
akaihola authored Aug 5, 2023
2 parents 524d05a + bd191dc commit 3ccd333
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
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

Check failure on line 18 in src/darker/fstring.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/fstring.py#L18

Constant name "State" doesn't conform to UPPER_CASE naming style (invalid-name, C0103)
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]

Check failure on line 75 in src/darker/fstring.py

View workflow job for this annotation

GitHub Actions / flake8

line too long (94 > 88 characters)
"""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`

Check failure on line 114 in src/darker/fstring.py

View workflow job for this annotation

GitHub Actions / flake8

line too long (89 > 88 characters)
: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

0 comments on commit 3ccd333

Please sign in to comment.