Files
learningcircuit--local-deep…/.pre-commit-hooks/check-research-id-type.py
T
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

111 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Pre-commit hook to check for incorrect research_id type hints.
Research IDs are UUIDs and should always be treated as strings, never as integers.
"""
import sys
import re
import os
from pathlib import Path
# Set environment variable for pre-commit hooks to allow unencrypted databases
os.environ["LDR_ALLOW_UNENCRYPTED"] = "true"
def check_file(filepath):
"""Check a single file for incorrect research_id patterns."""
errors = []
with open(filepath, "r", encoding="utf-8") as f:
lines = f.readlines()
# Patterns to check for
patterns = [
# Flask route with int type
(
r"<int:research_id>",
"Flask route uses <int:research_id> - should be <string:research_id>",
),
# Type hints with int
(
r"research_id:\s*int",
"Type hint uses research_id: int - should be research_id: str",
),
# Function parameters with int conversion
(
r"int\(research_id\)",
"Converting research_id to int - research IDs are UUIDs/strings",
),
# Integer comparison patterns
(
r"research_id\s*==\s*\d+",
"Comparing research_id to integer - research IDs are UUIDs/strings",
),
]
for line_num, line in enumerate(lines, 1):
# Skip comment and docstring lines — comments like
# "# Flask route: <int:research_id> (old API)" should not fire.
if line.lstrip().startswith(("#", '"""', "'''")):
continue
for pattern, message in patterns:
if re.search(pattern, line):
errors.append(f"{filepath}:{line_num}: {message}")
errors.append(f" {line.strip()}")
return errors
def main():
"""Main entry point."""
# Get files to check from command line arguments
files_to_check = sys.argv[1:]
if not files_to_check:
print("No files to check")
return 0
all_errors = []
for filepath in files_to_check:
# Skip non-Python files
if not filepath.endswith(".py"):
continue
# Skip test files, migration files, and pre-commit hooks (they might have legitimate int usage).
#
# Previously this used `"test_" in filepath` (bare substring) — that
# matched production files like protest_handler.py and missed the
# *_test.py convention and files under a /tests/ directory. Mirror
# the guard pattern from _is_raw_sql_exempt in custom-checks.py.
p = Path(filepath)
if (
p.name.startswith("test_")
or p.name.endswith("_test.py")
or "tests" in p.parts
or "migration" in filepath.lower()
or ".pre-commit-hooks" in filepath
):
continue
errors = check_file(filepath)
all_errors.extend(errors)
if all_errors:
print("Research ID type errors found:")
print("-" * 80)
for error in all_errors:
print(error)
print("-" * 80)
print(
f"Total errors: {len([e for e in all_errors if not e.startswith(' ')])}"
)
return 1
return 0
if __name__ == "__main__":
sys.exit(main())