Skip to content

Commit

Permalink
style: clarify .py file path argument names
Browse files Browse the repository at this point in the history
- `relative_path`: path relative to a `root` directory
- `path_from_cwd`: either relative to working directory
  or an absolute path
  • Loading branch information
akaihola committed Feb 3, 2025
1 parent 0297acb commit af49968
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/darker/import_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IsortArgs(TypedDict, total=False):

def apply_isort( # pylint: disable=too-many-arguments # noqa: PLR0913
content: TextDocument,
src: Path,
relative_path: Path,
root: Path,
exclude: Collection[str],
edited_linenums_differ: EditedLinenumsDiffer,
Expand All @@ -64,9 +64,9 @@ def apply_isort( # pylint: disable=too-many-arguments # noqa: PLR0913
"""Run isort on the given Python source file content
:param content: The contents of the Python source code file to sort imports in
:param src: The path to the file relative to ``root``. Note that this may differ
from the path given on the command line in
case of VSCode temporary files.
:param relative_path: The path to the file relative to ``root``. Note that this may
differ from the path given on the command line in case of
VSCode temporary files.
:param root: The root directory based on which ``src`` is resolved.
:param exclude: The file path patterns to exclude from import sorting
:param edited_linenums_differ: Helper for finding out which lines were edited
Expand All @@ -75,17 +75,17 @@ def apply_isort( # pylint: disable=too-many-arguments # noqa: PLR0913
:return: Original Python source file contents with imports sorted
"""
if glob_any(src, exclude):
if glob_any(relative_path, exclude):
return content
edited_linenums = edited_linenums_differ.revision_vs_lines(
src,
relative_path,
content,
context_lines=0,
) # pylint: disable=duplicate-code
if not edited_linenums:
return content
isort_args = _build_isort_args(root / src, config, line_length)
rev2_isorted = _call_isort_code(content, root / src, isort_args)
isort_args = _build_isort_args(root / relative_path, config, line_length)
rev2_isorted = _call_isort_code(content, root / relative_path, isort_args)
# Get the chunks in the diff between the edited and import-sorted file
isort_chunks = diff_chunks(content, rev2_isorted)
if not isort_chunks:
Expand All @@ -101,16 +101,16 @@ def apply_isort( # pylint: disable=too-many-arguments # noqa: PLR0913


def _build_isort_args(
src: Path,
path_from_cwd: Path,
config: Optional[str] = None,
line_length: Optional[int] = None,
) -> IsortArgs:
"""Build ``isort.code()`` keyword arguments
:param src: The path to the file. This must be either an absolute path or a relative
path from the current working directory. Note that this may differ from
the path given on the command line in
case of VSCode temporary files.
:param path_from_cwd: The path to the file. This must be either an absolute path or
a relative path from the current working directory. Note that
this may differ from the path given on the command line in
case of VSCode temporary files.
:param config: Path to configuration file
:param line_length: Maximum line length to use
Expand All @@ -119,19 +119,20 @@ def _build_isort_args(
if config:
isort_args["settings_file"] = config
else:
isort_args["settings_path"] = str(find_project_root((str(src),)))
isort_args["settings_path"] = str(find_project_root((str(path_from_cwd),)))
if line_length:
isort_args["line_length"] = line_length
return isort_args


def _call_isort_code(
content: TextDocument, src: Path, isort_args: IsortArgs
content: TextDocument, path_from_cwd: Path, isort_args: IsortArgs
) -> TextDocument:
"""Call ``isort.code()`` and return the result as a `TextDocument` object
:param content: The contents of the Python source code file to sort imports in
:param src: The path to the file with the given content
:param path_from_cwd: The path to the file with the given content, either relative
to the current working directory or an absolute path.
:param isort_args: Keyword arguments for ``isort.code()``
"""
Expand All @@ -141,7 +142,7 @@ def _call_isort_code(
", ".join(f"{k}={v!r}" for k, v in isort_args.items()),
)
with suppress(isort.exceptions.FileSkipped):
code = isort_code(code=code, file_path=src, **isort_args)
code = isort_code(code=code, file_path=path_from_cwd, **isort_args)
return TextDocument.from_str(
code,
encoding=content.encoding,
Expand Down

0 comments on commit af49968

Please sign in to comment.