Skip to content

Commit 8abd504

Browse files
committed
feat(version-scanner): implement targeted namespaced ignore pragmas
1 parent 055975c commit 8abd504

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

scripts/version_scanner/tests/unit/test_version_scanner.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,38 @@ def test_scan_file_ignores_pragma(tmp_path):
148148
results = scan_file(str(test_file), rules)
149149
assert len(results) == 0
150150

151+
152+
def test_scan_file_ignores_targeted_pragma(tmp_path):
153+
test_file = tmp_path / "test.py"
154+
test_file.write_text("python_requires = '>=3.7' # version-scanner: ignore-rule=python_requires_check:3.7\n")
155+
156+
rules = [
157+
{
158+
"name": "python_requires_check",
159+
"pattern": re.compile(r"python_requires\s*=\s*['\"]>=3\.7['\"]"),
160+
"version": "3.7"
161+
}
162+
]
163+
164+
results = scan_file(str(test_file), rules)
165+
assert len(results) == 0
166+
167+
168+
def test_scan_file_does_not_ignore_mismatched_targeted_pragma(tmp_path):
169+
test_file = tmp_path / "test.py"
170+
test_file.write_text("python_requires = '>=3.7' # version-scanner: ignore-rule=python_requires_check:3.8\n")
171+
172+
rules = [
173+
{
174+
"name": "python_requires_check",
175+
"pattern": re.compile(r"python_requires\s*=\s*['\"]>=3\.7['\"]"),
176+
"version": "3.7"
177+
}
178+
]
179+
180+
results = scan_file(str(test_file), rules)
181+
assert len(results) == 1
182+
151183
def test_scan_file_ignores_next_line(tmp_path):
152184
test_file = tmp_path / "test.py"
153185
test_file.write_text("# version-scanner: ignore-next-line\npython_requires = '>=3.7'\n")

scripts/version_scanner/version_scanner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,16 @@ def scan_file(file_path: str, compiled_rules: List[Dict[str, re.Pattern]]) -> Li
214214
if "version-scanner: ignore-next-line" in line:
215215
skip_next = True
216216
continue
217-
if "version-scanner: ignore" in line:
217+
if "version-scanner: ignore" in line and "version-scanner: ignore-rule" not in line and "version-scanner: ignore-next-line" not in line:
218218
continue
219219
for rule in compiled_rules:
220220
match = rule["pattern"].search(line)
221221
if match:
222+
version = rule.get("version")
223+
if version:
224+
pragma_pattern = rf"version-scanner\s*:\s*ignore-rule\s*=\s*{re.escape(rule['name'])}\s*:\s*{re.escape(version)}"
225+
if re.search(pragma_pattern, line, re.IGNORECASE):
226+
continue
222227
results.append({
223228
"rule_name": rule["name"],
224229
"line_number": line_num,

0 commit comments

Comments
 (0)