ReferenceSkillsPython

Python Standards

Auto-loads for projects with Python in their tech stack.

Metadata

FieldValue
Typecontext
Applies topython, fastapi, django, flask, pytest, pydantic, sqlalchemy, celery, poetry, asyncio, aiohttp, httpx

Core Principles

  1. Simplicity - Simple, understandable code
  2. Readability - Readability over cleverness
  3. Maintainability - Code that’s easy to maintain
  4. Testability - Code that’s easy to test
  5. DRY - Don’t Repeat Yourself

Naming Conventions

ElementConventionExample
Variables/Functionssnake_caseget_user_by_id, is_active
ClassesPascalCaseUserService, ApiClient
ConstantsUPPER_SNAKE_CASEMAX_RETRY_COUNT
PrivatePrefix with __internal_method
Files/Modulessnake_caseuser_service.py

Code Style

from dataclasses import dataclass
 
@dataclass
class User:
    id: str
    name: str
    email: str
    age: int | None = None
 
def get_user_by_id(user_id: str) -> User | None:
    if not user_id:
        raise ValueError("user_id cannot be empty")
    # implementation...

Best Practices

# Type hints everywhere
def process_items(items: list[str]) -> dict[str, int]:
    return {item: len(item) for item in items}
 
# Pydantic for validation
from pydantic import BaseModel, Field
 
class UserCreate(BaseModel):
    name: str = Field(..., min_length=2, max_length=50)
    email: EmailStr
    age: int | None = Field(None, ge=0, le=150)
 
# Prefer pathlib over os.path
from pathlib import Path
config_path = Path(__file__).parent / 'config.yaml'

Async/Await

async def fetch_user(user_id: str) -> User | None:
    async with httpx.AsyncClient() as client:
        response = await client.get(f"/users/{user_id}")
        return User(**response.json()) if response.status_code == 200 else None
 
# Gather for concurrent operations
async def fetch_all_users(user_ids: list[str]) -> list[User]:
    tasks = [fetch_user(uid) for uid in user_ids]
    return await asyncio.gather(*tasks)
ToolPurpose
uvPackage manager (faster than pip/poetry)
ruffLinting (replaces flake8, isort, black)
mypy or pyrightType checking
pytestTesting with pytest-cov, pytest-asyncio

Production Checklist

  1. Type hints everywhere
  2. Pydantic for validation
  3. Async for I/O operations
  4. Structured logging (not print)
  5. Environment variables via pydantic-settings
  6. Custom exceptions for domain errors
  7. Connection pooling for databases/HTTP