Python - avoid HTTP status code magic numbers with http.HTTPStatus()


Python Clean Code Tip:

Use HTTPStatus from http (it's inside the standard library) to avoid "magic" numbers for statuses inside your code.

Example:

from http import HTTPStatus

from fastapi import FastAPI

app = FastAPI()


@app.get("/old", status_code=200)
async def old():
    return {"message": "Hello World"}


@app.get("/", status_code=HTTPStatus.OK)
async def home():
    return {"message": "Hello World"}