cursordocs

Python Development Guidelines

1. Role Definition

2. Core Principles

3. Technical Specifications

3.1 Error Handling

from fastapi import HTTPException
from pydantic import BaseModel
from typing import Optional

class ErrorResponse(BaseModel):
    code: int
    message: str
    details: Optional[dict] = None

async def handle_error(error: Exception) -> ErrorResponse:
    if isinstance(error, HTTPException):
        return ErrorResponse(
            code=error.status_code,
            message=error.detail
        )
    return ErrorResponse(
        code=500,
        message="Internal Server Error",
        details={"type": type(error).__name__}
    )

3.2 FastAPI Development

Key Conventions:

  1. Leverage FastAPI’s dependency injection system for state and shared resource management
  2. Prioritize API performance metrics (response time, latency, throughput)
  3. Limit blocking operations in routes:
    • Favor asynchronous and non-blocking flows
    • Use dedicated async functions for database and external API operations
    • Structure routes and dependencies clearly for optimal readability and maintainability

3.3 Performance Optimization

4. Dependency Management

MUST read the LATEST library documents via context7 mcp server or cursor’s @Docs.

4.2 Dependency Management Tools

5. Toolchain

5.1 Essential Tools

5.2 Base Configuration

# pyproject.toml
[tool.black]
line-length = 120
target-version = ['py312']

[tool.mypy]
python_version = "3.12"
strict = true

6. Iteration Plan

Phase 1 (Current)

Phase 2

Phase 3

7. Key Points

  1. Simplicity First
    • Each specification has a clear purpose
    • Avoid over-engineering
    • Prioritize maintainability
  2. Progressive Enhancement
    • Ensure basic specifications are implemented
    • Expand based on actual requirements
    • Regular review and optimization
  3. Practicality Priority
    • Specifications serve actual development
    • Avoid formalism
    • Maintain flexibility

Last Updated: 2024-03-21 Version: 1.0.0