tezvyn:

Python & FastAPI

Python, Django, FastAPI, Flask, async Python

40 bites

Python & FastAPI30 sec read

FastAPI: Returning HTML with HTMLResponse

Override FastAPI's default JSON output by using `HTMLResponse` to return a raw HTML string directly from an endpoint. It's for simple status pages or server-side rendered components.

Python & FastAPI30 sec read

Pydantic Computed Fields: Serialize Derived Values

A Pydantic computed field makes a derived value, like an area from width and length, part of your model's serialized output. Use it to include calculated attributes when calling `.model_dump()`.

Python & FastAPI30 sec read

Pydantic: Configuring Models with `model_config`

Think of `model_config` as the settings panel for your Pydantic models, letting you change validation rules like string length or immutability. Use it to enforce global constraints or make models immutable. The footgun is using the old `class Config:` from V1.

Python & FastAPI30 sec read

Pydantic's Data Coercion: From Raw Data to Python Types

Pydantic automatically converts raw data, like strings from a JSON request, into the Python types you declare. It's how FastAPI turns a JSON body into a typed Python object.

Python & FastAPI30 sec read

Pydantic: Required vs. Optional Fields

In Pydantic, a field is required by default. To make it optional, you must provide a default value, like `name: str = "guest"` or `age: int | None = None`. This is key for flexible API request bodies.

Python & FastAPI30 sec read

FastAPI: Set a Response's HTTP Status Code

In FastAPI, set the success status code in the decorator, not the function. Use `status_code=201` in `@app.post()` to signal resource creation. The common footgun is placing `status_code` in the function signature instead of the decorator itself.

Python & FastAPI30 sec read

FastAPI: Validate Parameters with Query and Path

FastAPI's `Query` and `Path` objects let you declare rich validation rules directly in your function's signature. Enforce string lengths, regex patterns, or numeric ranges on URL parameters without writing manual checks.

Python & FastAPI30 sec read

FastAPI: Automatic Interactive API Docs

FastAPI turns your Python type hints into live, interactive API documentation. It generates an OpenAPI schema to power a UI where you can test endpoints directly from your browser, no extra work needed.

Python & FastAPI30 sec read

FastAPI: Pydantic for Robust Request Bodies

A Pydantic model is a contract for your API's request body. It tells FastAPI what data to expect, automatically converting incoming JSON into a typed Python object. Use this for any POST or PUT endpoint. The footgun is declaring path params in the body model.

Python & FastAPI30 sec read

Path Parameters: Turning URL Parts into Variables

Path parameters turn parts of a URL, like `/users/123`, into typed function arguments. FastAPI uses this to create endpoints for specific resources, like fetching a user by their ID. The footgun is forgetting type hints; without `int`, `123` is just a string.

Python & FastAPI30 sec read

FastAPI: Configure Endpoints with Decorators

FastAPI's path operation decorators configure an endpoint's metadata and behavior. Use them to set status codes (status_code=201), group endpoints with `tags`, or mark them as `deprecated`.

Python & FastAPI30 sec read

Accessing Python Type Annotations Safely

Accessing an object's type hints isn't just `obj.__annotations__`. Use `inspect.get_annotations()` in Python 3.10+ for safe access. This is key for tools like FastAPI that introspect your code.

Python & FastAPI30 sec read

Python's async/await: Concurrent, Not Parallel

async/await lets a single Python thread juggle multiple tasks, pausing one to work on another while it waits for I/O. It's ideal for network requests or database queries. The footgun: it won't speed up CPU-bound tasks, it only helps with waiting.

Python & FastAPI30 sec read

Python Coroutines: Functions You Can Pause and Resume

A Python coroutine is a function that can be paused and resumed. It yields control during I/O waits, allowing other tasks to run instead of blocking the program. The main footgun: calling an `async` function does nothing; you must `await` it to run it.

Python & FastAPI31 sec read

Python Enums: Give Names to Magic Numbers

Python's Enum gives meaningful names to "magic numbers" or strings. Use it for fixed sets of options like statuses or categories to make code self-documenting. The footgun: don't compare members to raw values; compare member to member for type safety.

Python & FastAPI30 sec read

Python Data Classes: Write Less Boilerplate

Python's @dataclass decorator writes boilerplate code like `__init__` and `__repr__` for you, turning a class with type hints into a data container. Use it for API payloads or simple records.

Python & FastAPI30 sec read

Python Type Hints: Documentation Your Linter Can Read

Type hints are labels for variables and function returns (`name: str`) that Python ignores at runtime. They enable static analysis tools and IDEs to catch errors before you run code.

Python & FastAPI30 sec read

Top 10 FastAPI interview questions for senior roles

From dependency injection internals to how Pydantic v2 validation differs from v1 — these are the interview questions that separate juniors from staff.

Python & FastAPI30 sec read

Async SQLAlchemy 2.0: the mental model that clicks

Sessions are not connections. Connections are not transactions. Once you internalise the three-layer model, async SQLAlchemy stops feeling magical.

Python & FastAPI30 sec read

FastAPI 0.118 ships first-class WebSocket auth

The new release adds dependency injection for WebSocket connections, so you can finally reuse your JWT auth dependencies on /ws routes without manual header parsing.