Python FastAPI Best Practices .cursorrules prompt file
About .cursorrules prompt file
What you can build
Scalable API Boilerplate: Develop a boilerplate template for scalable FastAPI applications. It should include best practices like a modular file structure, asynchronous database integration, error handling, and example routes with input validation.
API Performance Monitor: Create a service that helps developers monitor and analyze the performance of FastAPI applications. Include metrics for response time, latency, and throughput, with suggestions for optimization based on best practices.
Async ORM Wrapper: Build a lightweight, efficient wrapper around SQLAlchemy for seamless async database operations. Focus on integration with FastAPI and providing easy-to-use, declarative query interfaces.
Error Logging Middleware: Offer a customizable middleware for FastAPI applications that logs errors in a structured format. It should support integrations with popular logging services and provide insightful error metrics to developers.
Type Hint Validator: Develop a tool that scans Python/FastAPI codebases to ensure consistency and correctness in type hint usage. It should flag missing or incorrect type hints and suggest improvements.
Dynamic Route Tester: Create a web interface for dynamically testing FastAPI routes. It should allow users to provide request input, examine response output, and validate Pydantic models interactively.
Custom Error Factory Library: Provide a library for creating custom error types and factories in FastAPI applications. It should ensure consistent error handling and improve the clarity of error messages.
Async Data Caching System: Develop an efficient system for caching API responses and static data using Redis or in-memory store. Focus on easy integration with FastAPI and robust invalidation strategies.
Logging Middleware Interface: Design an intuitive user interface to configure logging middleware for FastAPI applications. Allow real-time updates and customization of logging levels and destinations.
Lifespan Manager Tool: Create a tool for managing FastAPI lifespan context managers, aiming to simplify the setup of startup and shutdown routines and reduce reliance on event decorators.
Benefits
- Emphasizes functional, declarative programming over classes, promoting concise and modular code structures.
- Prioritizes error handling with patterns like early returns and guard clauses for clear, efficient code flow.
- Advocates performance optimizations using async operations, caching, and lazy loading for scalable API development.
Synopsis
Developers building scalable APIs with FastAPI will benefit from this prompt to create efficient, asynchronous, and modular backend systems with proper error handling and performance optimization.
Overview of .cursorrules prompt
The .cursorrules file provides guidelines for developing scalable APIs using Python and FastAPI. It emphasizes writing concise, technical responses with accurate examples and suggests using functional programming over classes. The file promotes best practices such as modularization, iteration, and the use of descriptive variable names. It outlines a file structure strategy and advocates for the use of Pydantic models for input validation. Prioritizing error handling, the file suggests early returns and guard clauses to improve code readability. Dependencies mentioned include FastAPI, Pydantic v2, and async database libraries. It also includes FastAPI-specific guidelines focusing on performance optimization through asynchronous operations, caching, and middleware usage. Overall, the .cursorrules file emphasizes leveraging FastAPI's features like dependency injection to enhance API performance and maintainability.