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

feat: Add refurb linter #35

Merged
merged 5 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
76 changes: 51 additions & 25 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,54 @@ init_command = [
]

[[linter]]
code = 'PYUPGRADE'
is_formatter = true
include_patterns = [
'lintrunner_adapters/**/*.py',
'lintrunner_adapters/**/*.pyi',
]
exclude_patterns = []
command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pyupgrade_linter',
'--py37-plus',
'@{{PATHSFILE}}'
]
init_command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'pyupgrade==3.3.1',
]
code = 'PYUPGRADE'
is_formatter = true
include_patterns = [
'lintrunner_adapters/**/*.py',
'lintrunner_adapters/**/*.pyi',
]
exclude_patterns = []
command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pyupgrade_linter',
'--py37-plus',
'@{{PATHSFILE}}'
]
init_command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'pyupgrade==3.3.1',
]

[[linter]]
code = 'REFURB'
include_patterns = [
'lintrunner_adapters/**/*.py',
'lintrunner_adapters/**/*.pyi',
]
exclude_patterns = []
command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'refurb_linter',
'--',
'@{{PATHSFILE}}'
]
init_command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'refurb==1.10.0;python_version>="3.10"',
]
28 changes: 28 additions & 0 deletions examples/adapters/refurb/.lintrunner.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[[linter]]
code = 'REFURB'
include_patterns = [
'**/*.py',
]
exclude_patterns = []
command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'refurb_linter',
'--config-file=pyproject.toml',
'--show-disable',
'--severity=FURB101:advice',
'--severity=FURB102:warning',
'--',
'@{{PATHSFILE}}'
]
init_command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'refurb==1.10.0',
]
6 changes: 3 additions & 3 deletions lintrunner_adapters/adapters/flake8_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def get_issue_severity(code: str) -> LintSeverity:
# "T49": internal type checker errors or unmatched messages
if any(
code.startswith(x)
for x in [
for x in (
"B9",
"C4",
"C9",
Expand All @@ -143,13 +143,13 @@ def get_issue_severity(code: str) -> LintSeverity:
"E5",
"T400",
"T49",
]
)
):
return LintSeverity.ADVICE

# "F821": Undefined name
# "E999": syntax error
if any(code.startswith(x) for x in ["F821", "E999"]):
if any(code.startswith(x) for x in ("F821", "E999")):
return LintSeverity.ERROR

# "F": PyFlakes Error
Expand Down
6 changes: 3 additions & 3 deletions lintrunner_adapters/adapters/newlines_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def check_file(filename: str) -> LintMessage | None:
with open(filename, "rb") as f:
lines = f.readlines()

if len(lines) == 0:
if not lines:
# File is empty, just leave it alone.
return None

if len(lines) == 1 and len(lines[0]) == 1:
if len(lines) == len(lines[0]) == 1:
# file is wrong whether or not the only byte is a newline
return LintMessage(
path=filename,
Expand Down Expand Up @@ -77,7 +77,7 @@ def check_file(filename: str) -> LintMessage | None:
for idx, line in enumerate(lines):
if len(line) >= 2 and line[-1] == NEWLINE and line[-2] == CARRIAGE_RETURN:
if not has_changes:
original_lines = list(lines)
original_lines = lines.copy()
has_changes = True
lines[idx] = line[:-2] + b"\n"

Expand Down
167 changes: 167 additions & 0 deletions lintrunner_adapters/adapters/refurb_linter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""Refurbish and modernize Python code"""

from __future__ import annotations

import argparse
import logging
import re
import sys
import textwrap

from lintrunner_adapters import LintMessage, LintSeverity, add_default_options
from lintrunner_adapters._common.lintrunner_common import run_command

LINTER_CODE = "REFURB"

RESULTS_RE = re.compile(
r"""(?mx)
^
(?P<file>.*?):
(?P<line>\d+):
(?:(?P<column>-?\d+))?
(?:\s\[(?P<code>.*)\]:)?
\s(?P<message>.*)
$
"""
)


def _test_results_re() -> None:
"""Doctests.

>>> def t(s): return RESULTS_RE.search(s).groupdict()

>>> t(r"main.py:3:17 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`")
... # doctest: +NORMALIZE_WHITESPACE
{'file': 'main.py', 'line': '3', 'column': '17', 'code': 'FURB109',
'message': 'Use `in (x, y, z)` instead of `in [x, y, z]`'}

>>> t(r"main.py:4:5 [FURB101]: Use `y = Path(x).read_text()` instead of `with open(x, ...) as f: y = f.read()`")
... # doctest: +NORMALIZE_WHITESPACE
{'file': 'main.py', 'line': '4', 'column': '5', 'code': 'FURB101',
'message': 'Use `y = Path(x).read_text()` instead of `with open(x, ...) as f: y = f.read()`'}
"""
pass


def format_lint_message(message: str, code: str, show_disable: bool) -> str:
formatted = f"{message}"
if show_disable:
formatted += textwrap.dedent(
f"""

To disable, use
[tool.refurb]
ignore = [{code}]
or
disable = [{code}]
"""
)
return formatted


def check_files(
filenames: list[str],
severities: dict[str, LintSeverity],
*,
config_file: str,
retries: int,
show_disable: bool,
) -> list[LintMessage]:
try:
proc = run_command(
[sys.executable, "-mrefurb", "--config-file", config_file] + filenames,
retries=retries,
)
except OSError as err:
return [
LintMessage(
path=None,
line=None,
char=None,
code=LINTER_CODE,
severity=LintSeverity.ERROR,
name="command-failed",
original=None,
replacement=None,
description=(f"Failed due to {err.__class__.__name__}:\n{err}"),
)
]
stdout = str(proc.stdout, "utf-8").strip()
return [
LintMessage(
path=match["file"],
name=match["code"] or "note",
description=format_lint_message(
match["message"],
match["code"],
show_disable,
),
line=int(match["line"]),
char=int(match["column"])
if match["column"] is not None and not match["column"].startswith("-")
else None,
code=LINTER_CODE,
severity=severities.get(match["code"], LintSeverity.ADVICE),
original=None,
replacement=None,
)
for match in RESULTS_RE.finditer(stdout)
]


def main() -> None:
parser = argparse.ArgumentParser(
description=f"refurb wrapper linter. Linter code: {LINTER_CODE}. "
f"Use pyproject.toml to configure any refurb settings.",
fromfile_prefix_chars="@",
)
parser.add_argument(
"--config-file",
default="pyproject.toml",
help="path to pyproject.toml config file",
)
parser.add_argument(
"--severity",
action="append",
help="map code to severity (e.g. `FURB109:advice`). "
"This option can be used multiple times.",
)
parser.add_argument(
"--show-disable",
action="store_true",
help="show how to disable a lint message",
)
add_default_options(parser)
q0w marked this conversation as resolved.
Show resolved Hide resolved
args = parser.parse_args()

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,
stream=sys.stderr,
)

severities: dict[str, LintSeverity] = {}
if args.severity:
for severity in args.severity:
parts = severity.split(":", 1)
assert len(parts) == 2, f"invalid severity `{severity}`"
severities[parts[0]] = LintSeverity(parts[1])

lint_messages = check_files(
args.filenames,
severities,
config_file=args.config_file,
retries=args.retries,
show_disable=args.show_disable,
)
for lint_message in lint_messages:
lint_message.display()


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ disable = [
"unused-argument",
"unused-import",
]

[tool.refurb]
python_version = "3.7"
disable = ["FURB101", "FURB150"] # disable suggestions using pathlib
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Test dependencies to avoid doctest import errors
add-trailing-comma==2.4.0
pyupgrade==3.3.1
refurb==1.10.0;python_version>="3.10"
justinchuby marked this conversation as resolved.
Show resolved Hide resolved
ufmt==2.0.1