Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: Add pre-commit check for ORM models location

LearningCircuit97514d59a57b
diff --git a/.pre-commit-hooks/custom-checks.py b/.pre-commit-hooks/custom-checks.py
index 5690e8c593..d86f1afc3c 100755
--- a/.pre-commit-hooks/custom-checks.py
+++ b/.pre-commit-hooks/custom-checks.py
@@ -5,6 +5,7 @@ Checks for:
1. If loguru is used instead of standard logging
2. If logger.exception is used instead of logger.error for error handling
3. That no raw SQL is used, only ORM methods
+4. That ORM models (classes inheriting from Base) are defined in models/ folders
"""
import ast
@@ -20,6 +21,8 @@ class CustomCodeChecker(ast.NodeVisitor):
self.has_loguru_import = False
self.has_standard_logging_import = False
self.in_except_handler = False
+ self.has_base_import = False
+ self.has_declarative_base_import = False
def visit_Import(self, node):
for alias in node.names:
@@ -43,6 +46,18 @@ class CustomCodeChecker(ast.NodeVisitor):
)
elif node.module == "loguru":
self.has_loguru_import = True
+ elif node.module and "sqlalchemy" in node.module:
+ # Check for SQLAlchemy ORM imports
+ for name in node.names:
+ if name.name == "declarative_base":
+ self.has_declarative_base_import = True
+ # Also check for database.models.base imports
+ elif node.module and (
+ "models.base" in node.module or "models" in node.module
+ ):
+ for name in node.names:
+ if name.name == "Base":
+ self.has_base_import = True
self.generic_visit(node)
def visit_ExceptHandler(self, node):
@@ -69,6 +84,35 @@ class CustomCodeChecker(ast.NodeVisitor):
)
self.generic_visit(node)
+ def visit_ClassDef(self, node):
+ # Check if this class inherits from Base (SQLAlchemy model)
+ for base in node.bases:
+ base_name = ""
+ if isinstance(base, ast.Name):
+ base_name = base.id
+ elif isinstance(base, ast.Attribute):
+ base_name = base.attr
+
+ if base_name == "Base":
+ # This is an ORM model - check if it's in the models folder
+ if (
+ "/models/" not in self.filename
+ and not self.filename.endswith("/models.py")
+ ):
+ # Allow exceptions for test files and migrations
+ if not (
+ "test" in self.filename.lower()
+ or "migration" in self.filename.lower()
+ or "alembic" in self.filename.lower()
+ ):
+ self.errors.append(
+ (
+ node.lineno,
+ f"ORM model '{node.name}' should be defined in a models/ folder, not in {self.filename}",
+ )
+ )
+ self.generic_visit(node)
+
def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:
"""Check for raw SQL usage patterns."""
@@ -252,6 +296,13 @@ def main():
)
print(" - Allowed: session.query(), .filter(), .add(), etc.")
print(" - Raw SQL is permitted in migration files and schema tests")
+ print(
+ "4. Define ORM models (classes inheriting from Base) in models/ folders"
+ )
+ print(
+ " - Models should be in files like models/user.py or database/models/"
+ )
+ print(" - Exception: Test files and migration files")
sys.exit(1)
else:
print("✅ All custom checks passed!")