python:modern-python
Modern Python Features
Modern Python 3.10+ language features, type hints, and patterns.
Type Hints
Basic Types
def greet(name: str) -> str:
return f"Hello {name}"
age: int = 25
prices: list[float] = [9.99, 19.99]
mapping: dict[str, int] = {"a": 1}
Union Types (Python 3.10+)
# Modern syntax
def process(value: int | str) -> bool:
...
# Optional
def get_user(id: int) -> User | None:
...
# Multiple types
Result = int | str | bool
Generic Types
from typing import TypeVar, Generic
T = TypeVar("T")
class Repository(Generic[T]):
def get(self, id: int) -> T | None:
...
def save(self, entity: T) -> T:
...
user_repo = Repository[User]()
Protocol (Structural Typing)
from typing import Protocol
class Drawable(Protocol):
"""Structural type - any class with draw()"""
def draw(self) -> None:
...
def render(obj: Drawable) -> None:
"""Works with any object that has draw()"""
obj.draw()
Pattern Matching (Python 3.10+)
Basic Matching
def handle_command(command: str):
match command:
case "start":
start_process()
case "stop":
stop_process()
case "status":
return get_status()
case _:
raise ValueError("Unknown command")
Matching with Values
def handle_http_status(status: int):
match status:
case 200:
return "OK"
case 404:
return "Not Found"
case 500 | 502 | 503:
return "Server Error"
case _:
return "Unknown"
Structural Pattern Matching
def process_point(point):
match point:
case (0, 0):
return "Origin"
case (x, 0):
return f"On X-axis at {x}"
case (0, y):
return f"On Y-axis at {y}"
case (x, y):
return f"At ({x}, {y})"
Async/Await
Async Functions
async def fetch_data(url: str) -> dict:
"""Async function"""
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.json()
# Call async function
data = await fetch_data("https://api.example.com")
Concurrent Execution
import asyncio
async def process_all(urls: list[str]):
"""Process multiple URLs concurrently"""
tasks = [fetch_data(url) for url in urls]
results = await asyncio.gather(*tasks)
return results
Async Context Managers
class AsyncDatabase:
async def __aenter__(self):
await self.connect()
return self
async def __aexit__(self, *args):
await self.disconnect()
async with AsyncDatabase() as db:
await db.query(...)
Dataclasses and attrs
from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: float
y: float
def distance_from_origin(self) -> float:
return (self.x ** 2 + self.y ** 2) ** 0.5
Walrus Operator
# Assign and use in one expression
if (n := len(items)) > 10:
print(f"Too many items: {n}")
# In comprehensions
results = [y for x in items if (y := process(x)) is not None]
Modern String Formatting
name = "Alice"
age = 30
# f-strings
message = f"Hello {name}, you are {age} years old"
# f-strings with expressions
message = f"In 5 years you'll be {age + 5}"
# Debug f-strings (Python 3.8+)
print(f"{name=}") # name='Alice'
See references/ for advanced typing, async patterns, and pattern matching.
More from martinffx/claude-code-atelier
python:architecture
Python application architecture with functional core, effectful shell, DDD, and data modeling. Use when designing application layers, separating pure business logic from IO, defining domain models, implementing validation, or structuring bounded contexts.
14python:monorepo
Python monorepo architecture with uv workspaces, mise, and apps/packages pattern. Use when setting up project structure, configuring workspaces, managing dependencies across packages, or designing multi-app Python repositories.
13python:build-tools
Python project tooling with uv, mise, ruff, basedpyright, and pytest. Use when setting up pyproject.toml, running builds, typechecking, configuring tests, linting, formatting, or managing Python environments.
12python:testing
Stub-Driven TDD and layer boundary testing with pytest. Use when writing tests, deciding what to test, testing at component boundaries, or implementing test-driven development.
12python:sqlalchemy
SQLAlchemy ORM patterns for Python database access. Use when defining models, writing queries, implementing upserts, working with JSON columns, or managing database sessions.
12python:fastapi
Building REST APIs with FastAPI, Pydantic validation, and OpenAPI. Use when creating routes, handling requests, designing endpoints, implementing validation, error responses, pagination, or generating API documentation.
11