Problem: - Ruff removed 'from app.core.database import Base' from models/database/base.py - Import appeared "unused" (F401) but was actually a critical re-export - Caused ImportError: cannot import name 'Base' at runtime - Re-export pattern: import in one file to export from package Solution: 1. Added F401 ignore for models/database/base.py in pyproject.toml 2. Created scripts/verify_critical_imports.py verification script 3. Integrated verification into make check and CI pipeline 4. Updated documentation with explanation New Verification Script: - Checks all critical re-export imports exist - Detects import variations (parentheses, 'as' clauses) - Handles SQLAlchemy declarative_base alternatives - Runs as part of make check automatically Protected Files: - models/database/base.py - Re-exports Base for all models - models/__init__.py - Exports Base for Alembic - models/database/__init__.py - Exports Base from package - All __init__.py files (already protected) Makefile Changes: - make verify-imports - Run import verification - make check - Now includes verify-imports - make ci - Includes verify-imports in pipeline Documentation Updated: - Code quality guide explains re-export protection - Pre-commit workflow includes verification - Examples of why re-exports matter This prevents future issues where linters remove seemingly "unused" imports that are actually critical for application structure. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
# models/schema/cart.py
|
|
"""
|
|
Pydantic schemas for shopping cart operations.
|
|
"""
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
|
|
# ============================================================================
|
|
# Request Schemas
|
|
# ============================================================================
|
|
|
|
|
|
class AddToCartRequest(BaseModel):
|
|
"""Request model for adding items to cart."""
|
|
|
|
product_id: int = Field(..., description="Product ID to add", gt=0)
|
|
quantity: int = Field(1, ge=1, description="Quantity to add")
|
|
|
|
|
|
class UpdateCartItemRequest(BaseModel):
|
|
"""Request model for updating cart item quantity."""
|
|
|
|
quantity: int = Field(..., ge=1, description="New quantity (must be >= 1)")
|
|
|
|
|
|
# ============================================================================
|
|
# Response Schemas
|
|
# ============================================================================
|
|
|
|
|
|
class CartItemResponse(BaseModel):
|
|
"""Response model for a single cart item."""
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
product_id: int = Field(..., description="Product ID")
|
|
product_name: str = Field(..., description="Product name")
|
|
quantity: int = Field(..., description="Quantity in cart")
|
|
price: float = Field(..., description="Price per unit when added to cart")
|
|
line_total: float = Field(
|
|
..., description="Total price for this line (price * quantity)"
|
|
)
|
|
image_url: str | None = Field(None, description="Product image URL")
|
|
|
|
|
|
class CartResponse(BaseModel):
|
|
"""Response model for shopping cart."""
|
|
|
|
vendor_id: int = Field(..., description="Vendor ID")
|
|
session_id: str = Field(..., description="Shopping session ID")
|
|
items: list[CartItemResponse] = Field(
|
|
default_factory=list, description="Cart items"
|
|
)
|
|
subtotal: float = Field(..., description="Subtotal of all items")
|
|
total: float = Field(..., description="Total amount (currently same as subtotal)")
|
|
item_count: int = Field(..., description="Total number of items in cart")
|
|
|
|
@classmethod
|
|
def from_service_dict(cls, cart_dict: dict) -> "CartResponse":
|
|
"""
|
|
Create CartResponse from service layer dictionary.
|
|
|
|
This is a convenience method to convert the dictionary format
|
|
returned by cart_service into a proper Pydantic model.
|
|
"""
|
|
items = [CartItemResponse(**item) for item in cart_dict.get("items", [])]
|
|
return cls(
|
|
vendor_id=cart_dict["vendor_id"],
|
|
session_id=cart_dict["session_id"],
|
|
items=items,
|
|
subtotal=cart_dict["subtotal"],
|
|
total=cart_dict["total"],
|
|
item_count=len(items),
|
|
)
|
|
|
|
|
|
class CartOperationResponse(BaseModel):
|
|
"""Response model for cart operations (add, update, remove)."""
|
|
|
|
message: str = Field(..., description="Operation result message")
|
|
product_id: int = Field(..., description="Product ID affected")
|
|
quantity: int | None = Field(
|
|
None, description="New quantity (for add/update operations)"
|
|
)
|
|
|
|
|
|
class ClearCartResponse(BaseModel):
|
|
"""Response model for clearing cart."""
|
|
|
|
message: str = Field(..., description="Operation result message")
|
|
items_removed: int = Field(..., description="Number of items removed from cart")
|