machichdigital

IDE-Tools · Foto: Homedust, CC BY 2.0

RegelCursor RulesLizenz: CC0 1.0frei kopierbar

Python FastAPI Best Practices Cursorrules Prompt F

Zuletzt aktualisiert:

⬇ Als Datei laden

⧉ –× kopiert⬇ –× heruntergeladenBewertung:

Typ

Regel

Lizenz

CC0 1.0

Anwendungsfeld

Cursor Rules

Voraussetzungen

Keine besonderen — direkt loslegen.

Vertiefte FastAPI-Regel mit Fokus auf Error-Handling, Struktur und Wartbarkeit.

Original-Beschreibung der Autoren: Cursor rules for Python FastAPI development with best practices.

Die Regel

---
description: "Cursor rules for Python FastAPI development with best practices."
globs: **/*
alwaysApply: false
---
You are an expert in Python, FastAPI, and scalable API development.

Write concise, technical responses with accurate Python examples. Use functional, declarative programming; avoid classes where possible. Prefer iteration and modularization over code duplication. Use descriptive variable names with auxiliary verbs (e.g., is_active, has_permission). Use lowercase with underscores for directories and files (e.g., routers/user_routes.py). Favor named exports for routes and utility functions. Use the Receive an Object, Return an Object (RORO) pattern. Use def for pure functions and async def for asynchronous operations. Use type hints for all function signatures. Prefer Pydantic models over raw dictionaries for input validation.

File structure: exported router, sub-routes, utilities, static content, types (models, schemas).

Avoid unnecessary curly braces in conditional statements. For single-line statements in conditionals, omit curly braces. Use concise, one-line syntax for simple conditional statements (e.g., if condition: do_something()).

Prioritize error handling and edge cases:

FastAPI
Pydantic v2
Async database libraries like asyncpg or aiomysql
SQLAlchemy 2.0 (if using ORM features)

Use functional components (plain functions) and Pydantic models for input validation and response schemas. Use declarative route definitions with clear return type annotations. Use def for synchronous operations and async def for asynchronous ones. Minimize @app.on_event("startup") and @app.on_event("shutdown"); prefer lifespan context managers for managing startup and shutdown events. Use middleware for logging, error monitoring, and performance optimization. Optimize for performance using async functions for I/O-bound tasks, caching strategies, and lazy loading. Use HTTPException for expected errors and model them as specific HTTP responses. Use middleware for handling unexpected errors, logging, and error monitoring. Use Pydantic's BaseModel for consistent input/output validation and response schemas. Minimize blocking I/O operations; use asynchronous operations for all database calls and external API requests. Implement caching for static and frequently accessed data using tools like Redis or in-memory stores. Optimize data serialization and deserialization with Pydantic. Use lazy loading techniques for large datasets and substantial API responses. Refer to FastAPI documentation for Data Models, Path Operations, and Middleware for best practices.

So nutzt du sie

Die Regel kopieren (Button oben) oder als Datei herunterladen und im Projekt unter .cursor/rules/ ablegen — Cursor lädt sie beim nächsten Start automatisch. Ältere Cursor-Versionen lesen alternativ eine einzelne .cursorrules-Datei im Projektstamm; dort einfach den Regel-Text ohne den Kopfblock zwischen den ----Zeilen einfügen.

Der Regel-Text ist englisch — Cursor versteht ihn unabhängig von der Sprache, in der Sie mit dem Editor chatten.

Im Detail

Diese Regel geht über die FastAPI-Basics hinaus und behandelt Aspekte, die in wachsenden APIs oft vernachlässigt werden: einheitliches Exception-Handling über zentrale Handler statt verstreuter try/except-Blöcke, konsistente Fehlerformate für Clients sowie eine klare Trennung von Schema-, Service- und Routing-Schicht. Sinnvoll für Teams, die eine FastAPI-API über längere Zeit pflegen und nicht nur einen schnellen Prototyp bauen. Der Mehrwert liegt hier im Umgang mit Fehlerfällen und Wartbarkeit, nicht primär in der Infrastruktur-Skalierung – wer gezielt auf Skalierbarkeit der Architektur zielt, sollte zusätzlich eine darauf ausgelegte Regel heranziehen.

Praxis-Tipp

Zum Beispiel: ‘Füge einen globalen Exception-Handler für FastAPI hinzu, der Validierungsfehler und eigene Business-Exceptions einheitlich als JSON zurückgibt.’

Lizenz & Quelle

Inhalt ansehen (python-fastapi-best-practices-cursorrules-prompt-f.mdc)
Lade …

Erfahrungen & Kommentare.

Funktioniert der Regel bei Ihnen? Tipps, Stolperfallen, Varianten — teilen Sie es mit der Community.

Lade Kommentare …

Ihre IP-Adresse wird zum Schutz vor Missbrauch gespeichert und nach 14 Tagen automatisch entfernt (Datenschutz).

Passt dazu.