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(linter): resolve file path for the black linter to avoid a bug in black file path handling #92

Merged
merged 20 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ init_command = [
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'black==22.10.0',
'black==24.1.1',
'isort==5.10.1',
]
is_formatter = true
Expand Down
31 changes: 16 additions & 15 deletions lintrunner_adapters/adapters/black_isort_linter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Format file with black and isort."""

Check warning on line 1 in lintrunner_adapters/adapters/black_isort_linter.py

View workflow job for this annotation

GitHub Actions / lintrunner (ubuntu-latest, 3.10)

BLACK-ISORT format

Run `lintrunner -a` to apply this patch.

Check warning on line 1 in lintrunner_adapters/adapters/black_isort_linter.py

View workflow job for this annotation

GitHub Actions / lintrunner (macos-latest, 3.10)

BLACK-ISORT format

Run `lintrunner -a` to apply this patch.

# PyTorch LICENSE. See LICENSE file in the root directory of this source tree.

Expand All @@ -12,7 +12,7 @@
import sys

import lintrunner_adapters
from lintrunner_adapters import LintMessage, LintSeverity, as_posix, run_command
from lintrunner_adapters import LintMessage, LintSeverity, as_posix

LINTER_CODE = "BLACK-ISORT"

Expand All @@ -24,22 +24,22 @@
*,
fast: bool = False,
) -> list[LintMessage]:
del retries # Unused

try:
with open(filename, "rb") as f:
original = f.read()
with open(filename, "rb") as f:
# Run isort first then black so we get consistent result
# even if isort is not using the black profile
proc = run_command(
proc = subprocess.Popen(
Fixed Show fixed Hide fixed
[sys.executable, "-misort", "-"],
stdin=f,
retries=retries,
timeout=timeout,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
import_sorted = proc.stdout
# Pipe isort's result to black
proc = run_command(
# Launch second process and connect it to the first one
black_proc = subprocess.Popen(
Fixed Show fixed Hide fixed
[
sys.executable,
"-mblack",
Expand All @@ -50,12 +50,14 @@
filename,
"-",
],
stdin=None,
input=import_sorted,
retries=retries,
timeout=timeout,
check=True,
stdin=proc.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

# Let stream flow between them
replacement, _ = black_proc.communicate(timeout=timeout)

except subprocess.TimeoutExpired:
return [
LintMessage(
Expand All @@ -81,7 +83,7 @@
line=None,
char=None,
code=LINTER_CODE,
severity=LintSeverity.ADVICE,
severity=LintSeverity.ERROR,
name="command-failed",
original=None,
replacement=None,
Expand All @@ -103,7 +105,6 @@
)
]

replacement = proc.stdout
if original == replacement:
return []

Expand Down
Loading