Skip to content

Commit

Permalink
fix(linter): resolve file path for the black linter to avoid a bug in…
Browse files Browse the repository at this point in the history
… black file path handling (#92)

Fixes #93. This change uses `Path().resolve()` on the file path provided
to black to avoid a bug described in
psf/black#4209.
  • Loading branch information
justinchuby authored Feb 6, 2024
1 parent 327e653 commit 0053b50
Show file tree
Hide file tree
Showing 39 changed files with 301 additions and 203 deletions.
6 changes: 3 additions & 3 deletions .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==23.12.1', # Use 24.x when ruff styles are updated
'isort==5.10.1',
]
is_formatter = true
Expand Down Expand Up @@ -346,7 +346,7 @@ init_command = [
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'ruff==0.1.5',
'ruff==0.2.1',
]
is_formatter = true

Expand All @@ -373,7 +373,7 @@ init_command = [
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'ruff==0.1.5',
'ruff==0.2.1',
]
is_formatter = true

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/black_isort_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/black_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/clangformat_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
21 changes: 13 additions & 8 deletions examples/pytorch/clangtidy_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,12 @@ def check_file(
name=match["code"],
description=match["message"],
line=int(match["line"]),
char=int(match["column"])
if match["column"] is not None and not match["column"].startswith("-")
else None,
char=(
int(match["column"])
if match["column"] is not None
and not match["column"].startswith("-")
else None
),
code="CLANGTIDY",
severity=severities.get(match["severity"], LintSeverity.ERROR),
original=None,
Expand Down Expand Up @@ -228,11 +231,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/exec_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ def check_file(filename: str) -> Optional[LintMessage]:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
28 changes: 17 additions & 11 deletions examples/pytorch/flake8_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,11 @@ def check_files(
try:
proc = run_command(
[sys.executable, "-mflake8", "--exit-zero"] + filenames,
extra_env={"FLAKE8_PLUGINS_PATH": flake8_plugins_path}
if flake8_plugins_path
else None,
extra_env=(
{"FLAKE8_PLUGINS_PATH": flake8_plugins_path}
if flake8_plugins_path
else None
),
retries=retries,
)
except (OSError, subprocess.CalledProcessError) as err:
Expand Down Expand Up @@ -307,9 +309,11 @@ def check_files(
get_issue_documentation_url(match["code"]),
),
line=int(match["line"]),
char=int(match["column"])
if match["column"] is not None and not match["column"].startswith("-")
else None,
char=(
int(match["column"])
if match["column"] is not None and not match["column"].startswith("-")
else None
),
code="FLAKE8",
severity=severities.get(match["code"]) or get_issue_severity(match["code"]),
original=None,
Expand Down Expand Up @@ -353,11 +357,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/grep_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/isort_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
20 changes: 12 additions & 8 deletions examples/pytorch/mypy_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ def check_files(
name=match["code"],
description=match["message"],
line=int(match["line"]),
char=int(match["column"])
if match["column"] is not None and not match["column"].startswith("-")
else None,
char=(
int(match["column"])
if match["column"] is not None and not match["column"].startswith("-")
else None
),
code="MYPY",
severity=severities.get(match["severity"], LintSeverity.ERROR),
original=None,
Expand Down Expand Up @@ -158,11 +160,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/newlines_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ def check_file(filename: str) -> Optional[LintMessage]:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
8 changes: 5 additions & 3 deletions examples/pytorch/s3_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ def download(
DRY_RUN = True

logging.basicConfig(
format="[DRY_RUN] %(levelname)s: %(message)s"
if DRY_RUN
else "%(levelname)s: %(message)s",
format=(
"[DRY_RUN] %(levelname)s: %(message)s"
if DRY_RUN
else "%(levelname)s: %(message)s"
),
level=logging.INFO,
stream=sys.stderr,
)
Expand Down
12 changes: 7 additions & 5 deletions examples/pytorch/ufmt_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
1 change: 1 addition & 0 deletions lintrunner_adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Adapters and tools for lintrunner."""

from __future__ import annotations

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions lintrunner_adapters/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
to list available adapters.
"""

from __future__ import annotations

import json
Expand Down
12 changes: 7 additions & 5 deletions lintrunner_adapters/adapters/add_trailing_comma_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
19 changes: 13 additions & 6 deletions lintrunner_adapters/adapters/black_isort_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import concurrent.futures
import logging
import os
import pathlib
import subprocess
import sys

Expand Down Expand Up @@ -39,6 +40,10 @@ def check_file(
)
import_sorted = proc.stdout
# Pipe isort's result to black

# Resolve the file path to get around errors with Python 3.8/3.9 on Windows
# https://github.com/psf/black/issues/4209
resolved_filename = str(pathlib.Path(filename).resolve())
proc = run_command(
[
sys.executable,
Expand All @@ -47,7 +52,7 @@ def check_file(
*(("--ipynb",) if filename.endswith(".ipynb") else ()),
*(("--fast",) if fast else ()),
"--stdin-filename",
filename,
resolved_filename,
"-",
],
stdin=None,
Expand Down Expand Up @@ -143,11 +148,13 @@ def main() -> None:

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
level=(
logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO
),
stream=sys.stderr,
)

Expand Down
Loading

0 comments on commit 0053b50

Please sign in to comment.