Skip to content

Commit

Permalink
Care only if import sorting was done
Browse files Browse the repository at this point in the history
...not whether isort was run or not.

This is in preparation for unified exclusions, which is in preparation for `flynt`
support.
  • Loading branch information
akaihola committed Aug 27, 2022
1 parent daed1cd commit 13a37d7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Added
-----
- Add a CI workflow which verifies that the ``darker --help`` output in ``README.rst``
is up to date.
- Small simplification: It doesn't matter whether ``isort`` was run or not, only
whether changes were made.

Fixed
-----
Expand Down
9 changes: 5 additions & 4 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def _isort_and_blacken_single_file( # pylint: disable=too-many-arguments
)
else:
rev2_isorted = rev2_content
has_isort_changes = rev2_isorted != rev2_content
if relative_path_in_rev2 not in black_exclude:
# 9. A re-formatted Python file which produces an identical AST was
# created successfully - write an updated file or print the diff if
Expand All @@ -153,7 +154,7 @@ def _isort_and_blacken_single_file( # pylint: disable=too-many-arguments
edited_linenums_differ,
rev2_content,
rev2_isorted,
enable_isort,
has_isort_changes,
black_config,
)
else:
Expand All @@ -169,7 +170,7 @@ def _blacken_single_file( # pylint: disable=too-many-arguments,too-many-locals
edited_linenums_differ: EditedLinenumsDiffer,
rev2_content: TextDocument,
rev2_isorted: TextDocument,
enable_isort: bool,
has_isort_changes: bool,
black_config: BlackConfig,
) -> TextDocument:
"""In a Python file, reformat chunks with edits since the last commit using Black
Expand All @@ -182,7 +183,7 @@ def _blacken_single_file( # pylint: disable=too-many-arguments,too-many-locals
:param edited_linenums_differ: Helper for finding out which lines were edited
:param rev2_content: Contents of the file at ``revrange.rev2``
:param rev2_isorted: Contents of the file after optional import sorting
:param enable_isort: ``True`` if ``isort`` was already run for the file
:param has_isort_changes: ``True`` if ``isort`` was run and modified the file
:param black_config: Configuration to use for running Black
:return: Contents of the file after reformatting
:raise: NotEquivalentError
Expand Down Expand Up @@ -226,7 +227,7 @@ def _blacken_single_file( # pylint: disable=too-many-arguments,too-many-locals
edited_linenums = edited_linenums_differ.revision_vs_lines(
relative_path_in_repo, rev2_isorted, context_lines
)
if enable_isort and not edited_linenums and rev2_isorted == rev2_content:
if has_isort_changes and not edited_linenums:
logger.debug("No changes in %s after isort", absolute_path_in_rev2)
last_successful_reformat = rev2_isorted
break
Expand Down
4 changes: 2 additions & 2 deletions src/darker/tests/test_main_blacken_single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_blacken_single_file_common_ancestor(git_repo):
),
rev2_content=worktree,
rev2_isorted=worktree,
enable_isort=False,
has_isort_changes=False,
black_config={},
)

Expand Down Expand Up @@ -123,7 +123,7 @@ def docstring_func():
),
rev2_content=TextDocument.from_str(modified),
rev2_isorted=TextDocument.from_str(modified),
enable_isort=False,
has_isort_changes=False,
black_config={},
)

Expand Down

0 comments on commit 13a37d7

Please sign in to comment.