Skip to content

Commit a015856

Browse files
committed
fix(version_scanner): cast rule name and version to string in re.escape
1 parent 8abd504 commit a015856

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

scripts/version_scanner/tests/unit/test_version_scanner.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ def test_scan_file_ignores_targeted_pragma(tmp_path):
165165
assert len(results) == 0
166166

167167

168+
def test_scan_file_handles_non_string_rule_name_and_version(tmp_path):
169+
test_file = tmp_path / "test.py"
170+
test_file.write_text("python_requires = '>=3.7' # version-scanner: ignore-rule=123:3.7\n")
171+
172+
rules = [
173+
{
174+
"name": 123, # Integer rule name
175+
"pattern": re.compile(r"python_requires\s*=\s*['\"]>=3\.7['\"]"),
176+
"version": 3.7 # Float version
177+
}
178+
]
179+
180+
# This should not raise TypeError
181+
results = scan_file(str(test_file), rules)
182+
assert len(results) == 0
183+
184+
185+
168186
def test_scan_file_does_not_ignore_mismatched_targeted_pragma(tmp_path):
169187
test_file = tmp_path / "test.py"
170188
test_file.write_text("python_requires = '>=3.7' # version-scanner: ignore-rule=python_requires_check:3.8\n")

scripts/version_scanner/version_scanner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def scan_file(file_path: str, compiled_rules: List[Dict[str, re.Pattern]]) -> Li
221221
if match:
222222
version = rule.get("version")
223223
if version:
224-
pragma_pattern = rf"version-scanner\s*:\s*ignore-rule\s*=\s*{re.escape(rule['name'])}\s*:\s*{re.escape(version)}"
224+
pragma_pattern = rf"version-scanner\s*:\s*ignore-rule\s*=\s*{re.escape(str(rule['name']))}\s*:\s*{re.escape(str(version))}"
225225
if re.search(pragma_pattern, line, re.IGNORECASE):
226226
continue
227227
results.append({

0 commit comments

Comments
 (0)