Skip to content

Commit 04f45da

Browse files
authored
fix: better ALL behavior for sp-ruff-checks (#677)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1 parent 5c0bb86 commit 04f45da

4 files changed

Lines changed: 115 additions & 46 deletions

File tree

src/sp_repo_review/checks/ruff.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, ClassVar, Protocol
3+
from typing import TYPE_CHECKING, Any, ClassVar, Literal, Protocol
44

55
from .._compat import tomllib
66
from . import mk_url
@@ -15,14 +15,22 @@
1515
## R2xx: Ruff deprecations
1616

1717

18-
def get_rule_selection(ruff: dict[str, Any]) -> frozenset[str]:
19-
match ruff:
20-
case (
18+
def get_rule_selection(
19+
ruff: dict[str, Any], key: Literal["select", "ignore"] = "select"
20+
) -> frozenset[str]:
21+
match key, ruff:
22+
case "select", (
2123
{"lint": {"select": x} | {"extend-select": x}}
2224
| {"select": x}
2325
| {"extend-select": x}
2426
):
2527
return frozenset(x)
28+
case "ignore", (
29+
{"lint": {"ignore": x} | {"extend-ignore": x}}
30+
| {"ignore": x}
31+
| {"extend-ignore": x}
32+
):
33+
return frozenset(x)
2634
case _:
2735
return frozenset()
2836

src/sp_repo_review/ruff_checks/__main__.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@
1212
from .._compat import tomllib
1313
from ..checks.ruff import get_rule_selection, ruff
1414

15-
libs = {"AIR", "ASYNC", "DJ", "FAST", "INT", "NPY", "PD"}
16-
specialty = {
17-
"CPY", # preview only
18-
"A", # Naming related
19-
"N", # Naming related
20-
"ANN", # Not all rules good
21-
"TID", # Not all rules good
22-
"C90", # Complexity
23-
"COM", # Trailing commas inform the formatter
24-
"D", # Requires everything documented
25-
"DOC", # Style-specific
26-
"ERA", # Check for commented code
27-
"FBT", # Can't be applied to old code very well
28-
"FIX", # TODO's are okay
29-
"TD", # Picky on todo rules
30-
"INP", # Namespace packages are correct sometimes
31-
"S", # Security (can be picky)
32-
"SLF", # Very picky
33-
}
15+
# Create using ruff linter --output-format=json > src/sp_repo_review/ruff/linter.json
16+
RESOURCE_DIR = importlib.resources.files("sp_repo_review.ruff_checks")
17+
with RESOURCE_DIR.joinpath("linter.json").open(encoding="utf-8") as f:
18+
linter = json.load(f)
19+
20+
LINT_INFO = {r["prefix"]: r["name"] for r in linter if r["prefix"] not in {"", "F"}}
21+
LINT_INFO = dict(sorted(LINT_INFO.items()))
22+
23+
with RESOURCE_DIR.joinpath("select.json").open(encoding="utf-8") as f:
24+
select_info = json.load(f)
25+
LIBS = frozenset(select_info["libs"])
26+
SPECIALTY = frozenset(r["name"] for r in select_info["specialty"])
27+
28+
with RESOURCE_DIR.joinpath("ignore.json").open(encoding="utf-8") as f:
29+
IGNORE_INFO = json.load(f)
30+
31+
32+
def print_each(items: dict[str, str]) -> Iterator[str]:
33+
for k, v in items.items():
34+
kk = f'[green]"{k}"[/green],'
35+
yield f" {kk:23} [dim]# {v}[/dim]"
3436

3537

3638
def process_dir(path: Path) -> None:
@@ -55,32 +57,32 @@ def process_dir(path: Path) -> None:
5557
)
5658
raise SystemExit(2)
5759

58-
# Create using ruff linter --output-format=json > src/sp_repo_review/ruff/linter.json
59-
with (
60-
importlib.resources.files("sp_repo_review.ruff_checks")
61-
.joinpath("linter.json")
62-
.open(encoding="utf-8") as ff
63-
):
64-
linter = json.load(ff)
65-
66-
lint_info = {r["prefix"]: r["name"] for r in linter if r["prefix"] not in {"", "F"}}
67-
lint_info = dict(sorted(lint_info.items()))
68-
6960
if "ALL" in selected:
70-
selected = frozenset(lint_info.keys())
71-
72-
selected_items = {k: v for k, v in lint_info.items() if k in selected}
73-
all_uns_items = {k: v for k, v in lint_info.items() if k not in selected}
61+
ignored = get_rule_selection(ruff_config, "ignore")
62+
missed = [
63+
r
64+
for r in IGNORE_INFO
65+
if not any(
66+
x.startswith((r.get("rule", "."), r.get("family", ".")))
67+
for x in ignored
68+
)
69+
]
70+
71+
print('[green]"ALL"[/green] selected.')
72+
ignores = {v.get("rule", v.get("family", "")): v["reason"] for v in missed}
73+
if ignores:
74+
print("Some things that sometimes need ignoring:")
75+
for item in print_each(ignores):
76+
print(item)
77+
return
78+
79+
selected_items = {k: v for k, v in LINT_INFO.items() if k in selected}
80+
all_uns_items = {k: v for k, v in LINT_INFO.items() if k not in selected}
7481
unselected_items = {
75-
k: v for k, v in all_uns_items.items() if k not in libs | specialty
82+
k: v for k, v in all_uns_items.items() if k not in LIBS | SPECIALTY
7683
}
77-
libs_items = {k: v for k, v in all_uns_items.items() if k in libs}
78-
spec_items = {k: v for k, v in all_uns_items.items() if k in specialty}
79-
80-
def print_each(items: dict[str, str]) -> Iterator[str]:
81-
for k, v in items.items():
82-
kk = f'[green]"{k}"[/green],'
83-
yield f" {kk:23} [dim]# {v}[/dim]"
84+
libs_items = {k: v for k, v in all_uns_items.items() if k in LIBS}
85+
spec_items = {k: v for k, v in all_uns_items.items() if k in SPECIALTY}
8486

8587
panel_sel = Panel(
8688
"\n".join(print_each(selected_items)), title="Selected", border_style="green"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{ "rule": "ANN401", "family": "ANN", "reason": "Disallow Any" },
3+
{
4+
"rule": "PLC0415",
5+
"family": "PLC",
6+
"reason": "Import should be at top of file"
7+
},
8+
{ "rule": "PLR09", "family": "PLR", "reason": "Too many ..." },
9+
{
10+
"rule": "PLR2004",
11+
"family": "PLR",
12+
"reason": "Magic value used in comparison"
13+
},
14+
{
15+
"rule": "PT013",
16+
"family": "PT",
17+
"reason": "It's correct to import classes for typing!"
18+
},
19+
{
20+
"rule": "RUF009",
21+
"reason": "Too easy to get a false positive (function call in dataclass defaults)"
22+
},
23+
{ "family": "S", "reason": "Some subset is okay" },
24+
{ "family": "D", "reason": "Too many doc requests" },
25+
{ "family": "TD", "reason": "Todo format" },
26+
{ "family": "FIX", "reason": "Hacks and todos" },
27+
{ "rule": "TID252", "family": "TID", "reason": "Relative imports are fine" },
28+
{ "family": "COM", "reason": "Trailing commas teach the formatter" },
29+
{ "family": "A", "reason": "Okay to shadow builtins" },
30+
{ "rule": "E501", "family": "E", "reason": "Line too long" },
31+
{ "family": "C90", "reason": "Complexity" },
32+
{
33+
"rule": "SLF001",
34+
"family": "SLF",
35+
"reason": "Private members are okay to access"
36+
},
37+
{ "family": "ERA", "reason": "Commented out code" }
38+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"libs": ["AIR", "ASYNC", "DJ", "FAST", "INT", "NPY", "PD"],
3+
"specialty": [
4+
{ "name": "CPY", "reason": "preview only" },
5+
{ "name": "A", "reason": "Naming related" },
6+
{ "name": "N", "reason": "Naming related" },
7+
{ "name": "ANN", "reason": "Not all rules good" },
8+
{ "name": "TID", "reason": "Not all rules good" },
9+
{ "name": "C90", "reason": "Complexity" },
10+
{ "name": "COM", "reason": "Trailing commas inform the formatter" },
11+
{ "name": "D", "reason": "Requires everything documented" },
12+
{ "name": "DOC", "reason": "Style-specific" },
13+
{ "name": "ERA", "reason": "Check for commented code" },
14+
{ "name": "FBT", "reason": "Can't be applied to old code very well" },
15+
{ "name": "FIX", "reason": "TODO's are okay" },
16+
{ "name": "TD", "reason": "Picky on todo rules" },
17+
{ "name": "INP", "reason": "Namespace packages are correct sometimes" },
18+
{ "name": "S", "reason": "Security (can be picky)" },
19+
{ "name": "SLF", "reason": "Very picky" }
20+
]
21+
}

0 commit comments

Comments
 (0)