Get started¶
HeavySwag — a disruptor in the world of Python frameworks. Absolutely clean and fresh!
Install¶
To launch the application, you can install uvicorn, granian, and other HTTP servers.
The simplest app¶
from typing import NamedTuple
from heavyswag import HeavyRouter, HeavySwag, run_app
from heavyswag.specify import Request
router = HeavyRouter("/")
@router.get("/")
async def index(request: Request, dto: tuple[()]) -> str: # (1)!
return "Welcome!"
app = HeavySwag(main_router=router)
if __name__ == "__main__":
import uvicorn
uvicorn.run(run_app(app), host="127.0.0.1", port=8000)
-
HeavySwag is a strictly typed framework: every controller must declare an input DTO as its 2nd argument, even for routes that take no input at all. An empty
Tupleis the idiomatic "nothing to parse" DTO.tuple[()]requires Python 3.14On 3.12 and 3.13 it fails at startup — use an empty
NamedTuple(class Empty(NamedTuple): ...) instead. See Empty DTOs.Every controller has exactly the same shape:
- the 1st argument is always
Request(headers + cookies of the incoming request); - the 2nd argument is a DTO — a
NamedTuplethat HeavySwag builds for you from the path, the query string and the JSON body (see Request data below); - the return type can be a plain value (it gets wrapped into a
Responseautomatically) or aResponse[...]if you need to control status codes, headers or cookies (see Responses).
- the 1st argument is always
Run it and hit it:
Routing¶
HeavyRouter is the only routing primitive. The router you pass to
HeavySwag(main_router=...) must have "/" as its prefix — it's the
root of the whole route tree.
router = HeavyRouter("/")
router.get(path) # GET
router.post(path) # POST
router.put(path) # PUT
router.patch(path) # PATCH
router.delete(path) # DELETE
Path parameters¶
A {name} segment in the path becomes a path parameter, matched against the
DTO field with the same name:
Routes are resolved by a compressed radix tree, so static segments always
win over dynamic ones at the same depth — /items/active will match a
static /items/active route before it ever tries /items/{item_id},
regardless of the order you registered them in.
Nested routers¶
Group related routes under a prefix and attach them to the main router with
include_router:
users_router = HeavyRouter("/users")
main_router.include_router(users_router)
@users_router.get("/{user_id}")
async def get_user(request: Request, dto: UserIdDTO) -> str: ...
# -> GET /users/{user_id}
A router prefix must start with /, must not end with one, and may span
several segments (/api/v1) built from ASCII letters, digits and -, _,
., ~. A {name} segment is the one thing it may not contain — path
parameters belong on the route path.
Request data¶
DTOs are plain NamedTuples. Every field is resolved according to its
annotation:
| Annotation | Comes from |
|---|---|
Body[T] |
the JSON request body |
Query[T] |
the ?query=string |
| anything else | a path parameter ({name} in the route path) |
from typing import NamedTuple
from uuid import UUID
from heavyswag.specify import Body, Query, Request
class UpdateUser(NamedTuple):
user_id: UUID # path parameter -> /users/{user_id}
notify: Query[bool] # ?notify=true
age: Body[int] # {"age": 30, ...} in the JSON body
tags: Body[list[str]] # {"tags": ["a", "b"], ...}
@router.patch("/users/{user_id}")
async def update_user(request: Request, dto: UpdateUser) -> str:
return f"updated {dto.user_id}, age={dto.age}, tags={dto.tags}"
curl -X PATCH "http://127.0.0.1:8000/users/123e4567-e89b-12d3-a456-426614174000?notify=true" \
-H "Content-Type: application/json" \
-d '{"age": 30, "tags": ["vip", "new"]}'
Supported field types: str, int, float, bool, bytes, UUID,
datetime, and list[T] for Body fields (JSON arrays pass through as-is).
Every declared field is required
There's no "optional" marker yet — if a Body/Query field is declared
on the DTO and missing from the request, HeavySwag responds 400 Bad
Request before your controller ever runs. Design your DTOs (and which
HTTP verb you use) accordingly — a PATCH here means "send every
updatable field", not "send only what changed".
A malformed value (a non-numeric path segment for an int field, a broken
UUID, invalid JSON, ...) also short-circuits to 400 Bad Request — see
Error handling for how to customize this.
Responses¶
A controller can simply return a value — it gets wrapped into a 200
Response for you:
Return a Response[...] explicitly when you need a specific status code,
extra headers or cookies:
from heavyswag.specify import Response
@router.post("/users")
async def create_user(request: Request, dto: CreateUser) -> Response[str]:
response: Response[str] = Response(status_code=201)
response.set_body("created")
response.attach_header("X-Resource", "user")
return response
Content-Type¶
You don't set Content-Type yourself for the usual cases — HeavySwag picks
it from how the body is actually encoded, so the header always matches what
goes over the wire:
| The controller returns | Content-Type |
Body |
|---|---|---|
str |
text/plain; charset=utf-8 |
the string as-is |
bytes |
application/octet-stream |
the bytes as-is |
anything else — a NamedTuple, list[...], int, bool, … |
application/json |
serialized to JSON |
Two exceptions:
- you set it yourself — an explicit
Content-Typeheader (matched case-insensitively) is never overwritten. That's how you send HTML, XML, CSV, an image, or anything else the table doesn't cover; - there's no body — an empty body, or a
204/304response, gets noContent-Typeat all.
@router.get("/page")
async def page(request: Request, dto: Empty) -> Response[str]:
response: Response[str] = Response()
response.set_body("<h1>Hello</h1>")
response.attach_header("Content-Type", "text/html; charset=utf-8") # (1)!
return response
- Without this line the very same body would go out as
text/plain— the browser would show the markup instead of rendering it.
Why JSON isn't limited to NamedTuples
A list[User] or a bare 42 is serialized with json.dumps just like a
NamedTuple is, so it is JSON — labelling it text/plain would make
clients (and the generated API docs) treat valid JSON as an opaque
string. And bytes can be anything — an image, a PDF — so
application/octet-stream ("binary, type unknown") is the only honest
default; set the real type explicitly when you know it.
The OpenAPI generator documents responses by the very same rule, so the docs and the real responses never disagree.
A typical CRUD¶
A minimal in-memory "users" CRUD, showing path params, query params, the JSON body and every HTTP verb together:
from typing import NamedTuple
from uuid import UUID, uuid4
from heavyswag import HeavyRouter
from heavyswag.specify import Body, Query, Request, Response
users_router = HeavyRouter("/users")
_DB: dict[UUID, dict[str, str]] = {}
class Empty(NamedTuple): ... # (1)!
class UserId(NamedTuple):
user_id: UUID
class CreateUser(NamedTuple):
username: Body[str]
email: Body[str]
class UpdateUser(NamedTuple):
user_id: UUID
username: Body[str]
email: Body[str]
class ListUsers(NamedTuple):
search: Query[str]
- You can also create an empty structure in a similar way.
@users_router.get("/{user_id}")
async def get_user(request: Request, dto: UserId) -> Response[str]:
user = _DB.get(dto.user_id)
if user is None:
response: Response[str] = Response(status_code=404)
response.set_body("not found")
return response
response = Response()
response.set_body(f"{user['username']} <{user['email']}>")
return response
Wire it up:
from heavyswag import HeavyRouter, HeavySwag, run_app
from users import users_router
main_router = HeavyRouter("/")
main_router.include_router(users_router)
app = HeavySwag(main_router=main_router)
if __name__ == "__main__":
import uvicorn
uvicorn.run(run_app(app), host="127.0.0.1", port=8000)
curl -X POST http://127.0.0.1:8000/users \
-H "Content-Type: application/json" \
-d '{"username": "alice", "email": "alice@example.com"}'
curl "http://127.0.0.1:8000/users?search=ali"
curl http://127.0.0.1:8000/users/<user_id>
curl -X PATCH http://127.0.0.1:8000/users/<user_id> \
-H "Content-Type: application/json" \
-d '{"username": "alice2", "email": "alice2@example.com"}'
curl -X DELETE http://127.0.0.1:8000/users/<user_id>
Error handling¶
Every unhandled exception is caught before it reaches the ASGI boundary and
turned into a Response by an ErrorHandler. By default:
- any
HeavySwagError(and its subclasses, e.g.SerializationError) maps to a sensible status code; - anything else falls back to
500 Internal Server Error.
Register your own exception → (status_code, message) mapping:
from heavyswag.errors import HeavySwagError
from heavyswag.middlewares import ErrorHandler
class OutOfStockError(HeavySwagError):
pass
err_handler = ErrorHandler(
map_errors={OutOfStockError: (409, "Out of Stock")},
)
app = HeavySwag(main_router=main_router, err_handler=err_handler)
@router.get("/widgets/broken")
async def broken_widget(request: Request, dto: Empty) -> str:
raise OutOfStockError("widget 'broken' is out of stock")
The lookup walks the exception's MRO, so registering a base class also covers every subclass you didn't map explicitly — you don't need an entry for every single exception type, just the ones whose status code matters.
The response body is the mapped message as plain text:
$ curl -i http://127.0.0.1:8000/widgets/broken
HTTP/1.1 409 Conflict
content-type: text/plain; charset=utf-8
Out of Stock
A JSON error body¶
To send every error as one JSON shape instead, give ErrorHandler a
body factory. It's called with the exception, the mapped status code and
the mapped message, and returns the NamedTuple to send:
from typing import NamedTuple
from heavyswag.middlewares import ErrorHandler
class UsernameTakenError(Exception):
pass
class UnauthorizedError(Exception):
pass
class ErrorDTO(NamedTuple):
code: str
message: str
def error_body(exc: Exception, status_code: int, message: str) -> ErrorDTO: # (1)!
return ErrorDTO(code=type(exc).__name__, message=message)
app = HeavySwag(
main_router=main_router,
err_handler=ErrorHandler(
{
UsernameTakenError: (409, "Username already taken"),
UnauthorizedError: (401, "Unauthorized"),
},
body=error_body,
),
)
- The return annotation is required — a factory without one (a
lambda, say) is rejected whenErrorHandleris created. It's also the schema the API documentation shows for every error response.
Raise the exception anywhere in a controller — no Response to build:
@router.post("/users")
async def create_user(request: Request, dto: CreateUser) -> UserOut:
if username_exists(dto.username):
raise UsernameTakenError
...
$ curl -i -X POST http://127.0.0.1:8000/users -H "Content-Type: application/json" \
-d '{"username": "alex", ...}'
HTTP/1.1 409 Conflict
content-type: application/json
{"code": "UsernameTakenError", "message": "Username already taken"}
The factory covers every error, the built-in ones included — a malformed
request comes back as
{"code": "SerializationError", "message": "Bad Request"} with a 400, an
unmapped exception as {"code": "...", "message": "Internal Server Error"}
with a 500. So the client always gets the same shape.
The message comes from the mapping, not the exception
raise UsernameTakenError("alex is taken") still sends
"Username already taken" — the text you pass to the exception is never
shown to the client, so it's safe to put internal details there for
your logs. If the client should see per-request details, read them off
exc in the factory:
Middlewares¶
A middleware is anything shaped like:
async def middleware(call_next: CallNext, context: RequestContext) -> Response[Any]:
# do something before
response = await call_next(context)
# do something after
return response
— a plain function or a class with __call__ of that shape; no base class
to inherit from.
HeavySwag always runs three middlewares even if you don't ask for them:
CORS, error handling (see above) and request logging. Pass your own
instances in middlewares=[...] to replace the defaults, and anything else
you add runs closer to your controllers, after those three:
from heavyswag import HeavySwag
from heavyswag.middlewares import CORSMiddleware
from heavyswag.middlewares import LoggingMiddleware
app = HeavySwag(
main_router=main_router,
middlewares=[
CORSMiddleware(
allow_origins=["https://example.com"],
allow_methods=["GET", "POST", "PATCH", "DELETE"],
allow_credentials=True,
),
LoggingMiddleware(),
],
)
Writing your own¶
from typing import Any
from heavyswag.middlewares import CallNext, RequestContext
from heavyswag.specify import Response
class ApiKeyMiddleware:
def __init__(self, api_key: str) -> None:
self._api_key = api_key
async def __call__(
self, call_next: CallNext, context: RequestContext
) -> Response[Any]:
if context.preambule.url.startswith("/admin"):
if context.request.headers.get("x-api-key") != self._api_key:
response: Response[str] = Response(status_code=401)
response.set_body("Unauthorized")
return response
return await call_next(context)
RequestContext gives you .preambule (method + url + query string),
.request (headers + cookies) and .serializer (the low-level request/
response converter, rarely needed directly).
Cookies¶
Read them from the request, set them on the response:
from heavyswag.specify import Cookie, Request, Response
@router.post("/login")
async def login(request: Request, dto: Empty) -> Response[str]:
return Response( # (1)!
cookie={
Cookie(key="session_id", value="abc123", http_only=True, max_age=3600),
},
body="logged in",
)
@router.get("/profile")
async def profile(request: Request, dto: Empty) -> str:
session_id = request.cookies.get("session_id")
return f"session: {session_id}" if session_id else "not logged in"
- or initialize the response and then populate it
Cookie defaults to path="/", http_only=True and same_site=Lax —
override any of them per cookie.
Lifespan¶
lifespan runs setup code once before the server starts accepting requests,
and teardown code once after it stops accepting them — the yield is the
point where the app is "up" and serving traffic:
async def lifespan(app: HeavySwag) -> AsyncIterator[None]:
... # runs once, before the first request
yield
... # runs once, after the last request
This is the right place for anything expensive or stateful that a request handler shouldn't repeat on every call: loading an ML model into memory, opening a database connection pool, warming up a shared HTTP client, starting a background task, or resolving a dependency from a DI container. On the way out, it's where you release exactly those things — close connections, flush buffers, cancel background tasks.
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from heavyswag import HeavySwag
from app import jobs.send_event_to_kafka as task
from app import main_router
@asynccontextmanager
async def lifespan(app: HeavySwag) -> AsyncIterator[None]:
await task.run()
yield
await task.completion()
app = HeavySwag(main_router=main_router, lifespan=lifespan)
Graceful shutdown. An ASGI server only sends lifespan.shutdown after
it has stopped accepting new connections, which is exactly why the code
after yield is the right place to wind things down cleanly instead of
having them killed mid-flight: await, don't just cancel, anything still in
progress — a background task (task.completion() above), in-flight writes,
open connections — before the process actually exits.
lifespan is optional — a no-op default is used if you don't pass one.
Running¶
If you'd rather drive uvicorn from the CLI (e.g. for --reload during
development), expose the ASGI callable as a module-level attribute and point
uvicorn at it: