Modern Python Standards
Development practices for Python 3.10+ focusing on type safety, modern idioms, and efficient tooling.
Core Defaults
python
# Always use modern patterns
from pathlib import Path
from typing import Any
from dataclasses import dataclass
def process(items: list[dict[str, Any]]) -> dict[str, int] | None:
config_path = Path("config.json")
return {"count": len(items)} if items else None
Recommended Stack
| Category | Tool |
|---|---|
| Package Management | uv (preferred) or Poetry |
| Linting/Formatting | Ruff |
| Type Checking | mypy (strict mode) |
| Testing | pytest with coverage |
| Web APIs | FastAPI |
| Data Processing | Polars or Pandas |
Key Patterns
Type Hints
- •All public functions must have type hints
- •Use
X | Nonefor nullable values (3.10+) - •Prefer
list[X]overList[X](3.9+) - •Use
TypeVarfor generic functions
Async
- •Use
asynciowith modern patterns - •Avoid blocking in async contexts
- •
asyncio.gather()for concurrent operations - •
asyncio.TaskGroupfor structured concurrency (3.11+)
Path Handling
- •Always
pathlib.Pathoveros.path - •Use
.read_text(),.write_text() - •Proper path resolution, no hardcoding
Error Handling
- •Specific exceptions, never bare
except: - •Context managers for resources
- •Proper logging with structlog
Anti-Patterns to Avoid
| Bad | Good |
|---|---|
os.path.join() | Path() / "file" |
% formatting | f-strings |
pip install | uv add |
flake8 | Ruff |
List[str] | list[str] |
Optional[X] | X | None |
| Mutable default args | field(default_factory=list) |
See reference.md for detailed patterns and examples.md for code samples.