agentleFS
jaansokkjaansokk/cursor_tools/.cursor/skills/python-dev/SKILL.md

python-dev

Skill1 starsChanged 9 months agoMITView on GitHub

Python backend development: FastAPI, SQLAlchemy 2.0, Alembic, Pydantic v2, async Python, testing with pytest. Use when implementing or modifying API endpoints, database models/migrations, services, schemas, or backend tests.

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

28 rules. 0 are common, 12 are distinctive, and 16 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 12

    Distinctive

    This author's own choices. Worth a look.

  • 16

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this whenever you are asked to:

    line 16
  2. Distinctive:

    Write async services, integrations, or data pipelines

    line 20
  3. Distinctive:

    Write or update backend tests (pytest, pytest-asyncio)

    line 21
  4. Distinctive:

    Async discipline: no blocking calls in async paths

    line 27
  5. Distinctive:

    All I/O operations must be awaited — database queries, HTTP calls, file I/O.

    line 61
  6. Very specific:

    Never call blocking functions (e.g., time.sleep, synchronous DB drivers) inside async endpoints or services.

    line 62
  7. Very specific:

    Use asyncio.gather for independent concurrent operations.

    line 63
  8. Very specific:

    If you must call blocking code, use asyncio.to_thread() or a thread pool executor.

    line 64
  9. Very specific:

    Use Depends() for cross-cutting concerns: DB sessions, auth, config, services.

    line 68
  10. Distinctive:

    Use lifespan events for startup/shutdown (connection pools, caches).

    line 69
  11. Very specific:

    Prefer path operation decorators that declare response models: response_model=FooResponse.

    line 70
  12. Very specific:

    Use status_code parameter on decorators for non-200 success responses (201, 204).

    line 71
  13. Very specific:

    Use model_config = ConfigDict(from_attributes=True) for ORM-backed response models.

    line 77
  14. Very specific:

    Use Field() for constraints, defaults, descriptions, and examples.

    line 79
  15. Very specific:

    Prefer Annotated[str, Field(...)] style for reusable type annotations.

    line 80
  16. Very specific:

    Use declarative models with Mapped[] and mapped_column().

    line 84
  17. Very specific:

    Use select() statement style — never legacy session.query().

    line 85
  18. Very specific:

    Prefer session.scalars(select(...)) over session.execute(select(...)).scalars().

    line 86
  19. Very specific:

    Use relationships with lazy="selectin" or explicit eager loading to avoid N+1 queries.

    line 87
  20. Distinctive:

    Always scope sessions properly — one session per request via dependency injection.

    line 88
  21. Very specific:

    Run alembic revision --autogenerate -m "description" after model changes.

    line 92
  22. Distinctive:

    Always review autogenerated migrations — they can miss renames, data migrations, or constraints.

    line 93
  23. Very specific:

    Test migrations forward and backward (upgrade + downgrade).

    line 95
  24. Very specific:

    Use HTTPException for expected errors (validation, not-found, auth failures).

    line 99
  25. Distinctive:

    Use exception handlers for domain-specific error classes.

    line 100
  26. Distinctive:

    Never expose internal details (tracebacks, DB errors) in API responses.

    line 103
  27. Distinctive:

    Keep business logic in service modules, not in endpoint functions.

    line 107
  28. Distinctive:

    Avoid circular imports — use dependency injection and interfaces.

    line 109

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Python Development

When to use this skill

Use this whenever you are asked to:

  • Build or modify FastAPI endpoints, middleware, or dependencies
  • Work with SQLAlchemy models, queries, or Alembic migrations
  • Define or update Pydantic schemas (request/response, config, validation)
  • Write async services, integrations, or data pipelines
  • Write or update backend tests (pytest, pytest-asyncio)

Outcome targets

  • Correctness: endpoints return proper status codes; data integrity maintained
  • Type safety: type hints everywhere; Pydantic at all boundaries
  • Async discipline: no blocking calls in async paths
  • Testability: behavior-focused tests; mocked external dependencies
  • Clarity: small functions, meaningful errors, structured logging

Stack and tooling

  • FastAPI — async endpoints, dependency injection, lifespan events, middleware
  • Pydantic v2 — schemas, settings, validation, serialization
  • SQLAlchemy 2.0 — async sessions, declarative models, Mapped[], select() style
  • Alembic — async migrations, autogenerate, manual revision editing
  • Async Python — asyncio, asyncpg, httpx, proper concurrency patterns
  • Testing — pytest, pytest-asyncio, httpx AsyncClient, fixtures, coverage

Coding standards

  1. Async-first — all I/O (DB, HTTP, LLM) must be async. No blocking calls in async paths.
  2. Type hints everywhere — use modern Python syntax (PEP 604 unions, generics, etc.).
  3. Pydantic at all boundaries — request/response models, config, inter-module data. No raw dicts across boundaries.
  4. SQLAlchemy 2.0 only — no legacy 1.x Query API. Use select(), Mapped[], mapped_column().
  5. Dependency injection — use FastAPI Depends() for sessions, config, auth, services.
  6. Structured logging — use the project's logging setup, not bare print().
  7. Meaningful errors — HTTPException with correct status codes and clear detail messages.
  8. Small, focused functions — single responsibility. Easy to read, easy to test.

Best-practice rules

A) Async discipline

  • All I/O operations must be awaited — database queries, HTTP calls, file I/O.
  • Never call blocking functions (e.g., time.sleep, synchronous DB drivers) inside async endpoints or services.
  • Use asyncio.gather for independent concurrent operations; avoid creating tasks you don't track.
  • If you must call blocking code, use asyncio.to_thread() or a thread pool executor.

B) FastAPI patterns

  • Use Depends() for cross-cutting concerns: DB sessions, auth, config, services.
  • Use lifespan events for startup/shutdown (connection pools, caches).
  • Prefer path operation decorators that declare response models: response_model=FooResponse.
  • Use status_code parameter on decorators for non-200 success responses (201, 204).
  • Group related endpoints in routers; keep main.py thin.

C) Pydantic schemas

  • Separate request and response models — don't reuse the same schema for both.
  • Use model_config = ConfigDict(from_attributes=True) for ORM-backed response models.
  • Validate early: let Pydantic reject bad input at the API boundary, not deep in business logic.
  • Use Field() for constraints, defaults, descriptions, and examples.
  • Prefer Annotated[str, Field(...)] style for reusable type annotations.

D) SQLAlchemy 2.0

  • Use declarative models with Mapped[] and mapped_column().
  • Use select() statement style — never legacy session.query().
  • Prefer session.scalars(select(...)) over session.execute(select(...)).scalars().
  • Use relationships with lazy="selectin" or explicit eager loading to avoid N+1 queries.
  • Always scope sessions properly — one session per request via dependency injection.

E) Alembic migrations

  • Run alembic revision --autogenerate -m "description" after model changes.
  • Always review autogenerated migrations — they can miss renames, data migrations, or constraints.
  • For data migrations, write explicit op.execute() SQL in the migration.
  • Test migrations forward and backward (upgrade + downgrade).

F) Error handling

  • Use HTTPException for expected errors (validation, not-found, auth failures).
  • Use exception handlers for domain-specific error classes.
  • Return consistent error response shapes across the API.
  • Log unexpected errors with full context; return generic 500 to clients.
  • Never expose internal details (tracebacks, DB errors) in API responses.

G) Project structure

  • Keep business logic in service modules, not in endpoint functions.
  • Endpoints should: validate input, call services, return responses.
  • Avoid circular imports — use dependency injection and interfaces.
  • Group by feature/domain, not by layer, when the project grows.

Testing

  • Write tests alongside implementation — new endpoints, services, and schema changes should include corresponding tests.
  • Test behavior, not internals — test inputs -> outputs, status codes, side effects.
  • Use fixtures for shared setup — DB sessions, test clients, sample data. Keep conftest.py organized by scope.
  • Async tests — use pytest-asyncio and httpx.AsyncClient for endpoint tests.
  • Mock external dependencies — LLM calls, external HTTP, scheduled jobs. Never hit real external services in tests.
  • Cover the important paths — happy path, validation errors (422), auth failures (401/403), not-found (404), and domain-specific edge cases.
  • Run tests after changes — if tests exist in the area you touched, run them and fix failures before finishing.
  • Coverage awareness — prioritize coverage for business logic and API boundaries over boilerplate.

Common failure modes (fast triage)

SymptomLikely causeFix
RuntimeWarning: coroutine was never awaitedMissing await on async callAdd await
Endpoint hangs / slow responseBlocking call in async pathReplace with async equivalent or to_thread()
DetachedInstanceErrorAccessing lazy-loaded attribute outside sessionUse eager loading or access within session scope
N+1 queriesLazy loading in a loopUse selectin loading or explicit joins
Migration autogenerate misses changeRename or complex alterationWrite manual migration operations
422 with unclear messagePydantic validation errorCheck schema constraints and field types
Circular importModules importing each other at top levelUse TYPE_CHECKING guard or restructure

Sources

FastAPI

Pydantic

SQLAlchemy

Alembic

Testing

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.