Skip to content

Commit e0d3945

Browse files
committed
feat(version-scanner): implement targeted namespaced ignore pragmas
1 parent 04f3d2d commit e0d3945

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
@@ -146,6 +146,38 @@ def test_scan_file_ignores_pragma(tmp_path):
146146
results = scan_file(str(test_file), rules)
147147
assert len(results) == 0
148148

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