(code: str, filename: str = "model_loader.py")
| 79 | |
| 80 | |
| 81 | def run_pyspector_ai(code: str, filename: str = "model_loader.py") -> list[dict]: |
| 82 | try: |
| 83 | from pyspector._rust_core import run_scan |
| 84 | from pyspector.cli import AstEncoder |
| 85 | from pyspector.config import get_default_rules |
| 86 | except (ImportError, SystemExit) as exc: |
| 87 | pytest.skip(f"PySpector Rust core is not available: {exc}") |
| 88 | |
| 89 | wrapped = _wrap(code) |
| 90 | rules_toml = get_default_rules(ai_scan=True) |
| 91 | |
| 92 | with tempfile.TemporaryDirectory() as tmpdir: |
| 93 | path = os.path.join(tmpdir, filename) |
| 94 | Path(path).write_text(wrapped) |
| 95 | with warnings.catch_warnings(): |
| 96 | warnings.filterwarnings("ignore") |
| 97 | try: |
| 98 | ast_json = json.dumps(ast.parse(wrapped), cls=AstEncoder) |
| 99 | except Exception: |
| 100 | ast_json = "{}" |
| 101 | files = [{"file_path": filename, "content": wrapped, "ast_json": ast_json}] |
| 102 | results = run_scan(tmpdir, rules_toml, {"exclude": []}, files) |
| 103 | |
| 104 | return [{"rule_id": result.rule_id, "line_number": result.line_number} for result in results] |
| 105 | |
| 106 | |
| 107 | def fires(code: str, rule_id: str) -> bool: |
no test coverage detected