Skip to content

Commit 95d006d

Browse files
authored
Fix incorrect script for no-pylint-disable (#178)
Our current script returns false positives for stacked PRs. Upgrading it to the latest one from ucx
1 parent 19a5529 commit 95d006d

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

.github/workflows/no-cheat.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ jobs:
1515

1616
- name: Verify no lint is disabled in the new code
1717
run: |
18-
NEW_CODE=$(git diff origin/main..$(git branch --show-current) | grep -e '^+')
19-
CHEAT=$(echo "${NEW_CODE}" | grep '# pylint: disable' | grep -v "CHEAT" | wc -c)
20-
if [ "${CHEAT}" -ne 0 ]; then
21-
echo "Do not cheat the linter: ${CHEAT}"
18+
git fetch origin $GITHUB_BASE_REF:$GITHUB_BASE_REF
19+
git diff $GITHUB_BASE_REF...$(git branch --show-current) >> diff_data.txt
20+
python tests/unit/no_cheat.py diff_data.txt >> cheats.txt
21+
COUNT=$(cat cheats.txt | wc -c)
22+
if [ ${COUNT} -gt 1 ]; then
23+
cat cheats.txt
2224
exit 1
23-
fi
25+
fi

tests/unit/no_cheat.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
from pathlib import Path
3+
4+
DISABLE_TAG = '# pylint: disable='
5+
6+
7+
def no_cheat(diff_text: str) -> str:
8+
lines = diff_text.split('\n')
9+
removed: dict[str, int] = {}
10+
added: dict[str, int] = {}
11+
for line in lines:
12+
if not (line.startswith("-") or line.startswith("+")):
13+
continue
14+
idx = line.find(DISABLE_TAG)
15+
if idx < 0:
16+
continue
17+
codes = line[idx + len(DISABLE_TAG) :].split(',')
18+
for code in codes:
19+
code = code.strip().strip('\n').strip('"').strip("'")
20+
if line.startswith("-"):
21+
removed[code] = removed.get(code, 0) + 1
22+
continue
23+
added[code] = added.get(code, 0) + 1
24+
results: list[str] = []
25+
for code, count in added.items():
26+
count -= removed.get(code, 0)
27+
if count > 0:
28+
results.append(f"Do not cheat the linter: found {count} additional {DISABLE_TAG}{code}")
29+
return '\n'.join(results)
30+
31+
32+
if __name__ == "__main__":
33+
diff_data = sys.argv[1]
34+
path = Path(diff_data)
35+
if path.exists():
36+
diff_data = path.read_text("utf-8")
37+
print(no_cheat(diff_data))

0 commit comments

Comments
 (0)