Python FastAPI Best Practices .cursorrules prompt file
About .cursorrules prompt file
What you can build
Scalable API Development Platform: Build a cloud-based platform that simplifies the development of scalable APIs using Python and FastAPI. It should include templates and modules for setting up API routes, input validation with Pydantic, middleware for error handling, and performance monitoring.
Async Data Processing Library: Create a Python library focused on asynchronous data processing tasks, leveraging FastAPI's async capabilities. The library would include utilities for handling asynchronous database operations, integration with async libraries like asyncpg, and caching mechanisms.
API Performance Analyzer: Develop a tool that analyzes FastAPI applications for performance bottlenecks. It should focus on response time, latency, and throughput metrics, providing recommendations for optimizing asynchronous flows and reducing blocking operations.
Pydantic Validation Toolkit: Offer a toolkit that enhances Pydantic validation features, providing advanced error handling and logging capabilities. This toolkit could include plugins for custom error types and validation schemas for complex data structures.
FastAPI Middleware Extensions: Create a set of middleware extensions for FastAPI, focusing on logging, error monitoring, and performance optimization. These would include tools for managing startup/shutdown events, HTTP error responses, and performance metrics.
API Error Handling Framework: Design a framework that standardizes error handling in FastAPI applications. This framework should offer consistent error messages, logging strategies, and error monitoring, utilizing custom error types and factories.
Lazy Data Loading Service: Build a service that facilitates lazy loading of large datasets in FastAPI applications. This could include APIs and utilities for managing paginated responses and on-demand data fetching strategies.
Database Interaction ORM: Develop a lightweight ORM optimized for asynchronous database interactions in FastAPI using SQLAlchemy 2.0, focusing on reducing blocking operations and caching frequently accessed data.
Declarative Route Builder: Provide a tool for building FastAPI routes using a declarative syntax that emphasizes type safety, clear return type annotations, and modular components. This could streamline route definitions and enhance maintainability.
API Caching System: Implement a caching system tailored for FastAPI APIs, using tools like Redis or in-memory stores to efficiently manage cacheable responses and static content, improving performance and reducing latency.
Benefits
- Emphasizes functional, declarative programming with detailed Python example usage, avoiding classes for cleaner, more modular code.
- Comprehensive error handling strategy, prioritizing early returns and use of guard clauses for improved code readability and stability.
- Strong focus on performance optimization including async operations, caching techniques, and lazy loading for robust API development.
Synopsis
Developers creating scalable APIs with FastAPI will benefit from this prompt to design performant, modular, and maintainable services using Python and modern asynchronous techniques.
Overview of .cursorrules prompt
The .cursorrules file outlines best practices and guidelines for Python and FastAPI development, emphasizing scalable API solutions. It covers principles like functional and declarative programming, error handling, and performance optimization. It recommends concise and accurate Python examples, type hints, Pydantic models for validation, and asynchronous operations. Developers are encouraged to use FastAPI's dependency injection and middleware for improved performance and maintainability, with specific focus on managing startup and shutdown processes efficiently and employing caching strategies. The file prioritizes readability, modularization, and error logging, along with leveraging FastAPI-specific features like Pydantic models for consistency.