Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

8664 bites

Page 2

Python Async Context Managers
Python & FastAPI2 min read

Python Async Context Managers

Async context managers let you await during setup and teardown. Use async with for database connections or streams where acquiring and releasing both need I/O. The footgun is applying @contextmanager to async cleanup, which cannot await and will crash.

Python & FastAPI2 min read

What is the difference between a path parameter and a query parameter?

Tests REST API design and FastAPI binding. A strong answer states path params identify resources in the URL while query params filter after the question mark, then codes user_id in the route and q: str | None = None in the function.

Accessing Python Type Annotations Safely
Python & FastAPI2 min 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 & FastAPI2 min read

How does FastAPI distinguish required optional and default query parameters

Tests whether you know FastAPI infers query parameter optionality from Python signature defaults. Answer: no default means required, Optional[T] = None means optional, T = value sets a default, with all three in one signature.

Python Concurrency vs. Parallelism
Python & FastAPI2 min read

Python Concurrency vs. Parallelism

Concurrency is juggling tasks; parallelism is doing them at once. In Python, use concurrency (threading/asyncio) for I/O-bound work like API calls, and parallelism (multiprocessing) for CPU-bound tasks.

Python & FastAPI2 min read

What standard and code elements power FastAPI's auto-generated API docs?

Tests whether you know FastAPI uses the OpenAPI standard and extracts metadata from Python type hints, Pydantic models, decorators, and docstrings to build interactive docs. Red flag: claiming you must manually maintain a separate schema file.

The Python GIL: One Thread at a Time
Python & FastAPI2 min read

The Python GIL: One Thread at a Time

The Python Global Interpreter Lock (GIL) is a mutex ensuring only one thread executes Python bytecode at a time. This serializes CPU-bound threads, but the lock is released during I/O, making it effective for network-bound tasks.

Python & FastAPI2 min read

FastAPI non-integer query param default behavior

Tests FastAPI's automatic Pydantic validation and default error contracts. Strong answer: 422 Unprocessable Entity with JSON detail array containing loc, msg, and type fields. Red flag: saying 400 Bad Request or manual validation is needed.

FastAPI Application Instance: Your API's Central Hub
Python & FastAPI2 min read

FastAPI Application Instance: Your API's Central Hub

The FastAPI instance is your API's central switchboard, connecting incoming requests to your code. You create it once (e.g., app = FastAPI()) and use its decorators like @app.get to define all your endpoints. The footgun is creating multiple instances.

How do you type-hint repeated query params in FastAPI?
Python & FastAPI2 min read

How do you type-hint repeated query params in FastAPI?

Tests FastAPI's Annotated pattern for multi-value query strings. A great answer uses Annotated[list[str], Query()] = [] to collect repeated keys, and notes the old Query-as-default alternative. Red flag: manual parsing or typing it as str.

FastAPI: Configure Endpoints with Decorators
Python & FastAPI2 min 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.

What is the :path converter in FastAPI?
Python & FastAPI2 min read

What is the :path converter in FastAPI?

Tests FastAPI routing semantics and URL segmentation. A strong answer states :path captures slashes across segments while plain str stops at the next slash, and cites file-serving as the use case.

Path Parameters: Turning URL Parts into Variables
Python & FastAPI2 min 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.

How do you define a Pydantic model for FastAPI request body validation?
Python & FastAPI2 min read

How do you define a Pydantic model for FastAPI request body validation?

Subclass BaseModel with id int, email str, full_name str|None; pass it as a route param so FastAPI validates JSON and returns 422s.

Python & FastAPI2 min read

FastAPI Query Parameters: Beyond the URL Path

In FastAPI, function arguments not in the URL path become query parameters—the optional key-value pairs after a URL's ?. Use them for filtering or pagination, like /items?skip=0&limit=10. The footgun: omitting a default value makes the parameter required.

How does Pydantic handle extra JSON fields, and how to configure it?
Python & FastAPI2 min read

How does Pydantic handle extra JSON fields, and how to configure it?

This tests Pydantic's data filtering behavior and configuration. By default, Pydantic ignores extra fields silently. Set model_config = ConfigDict(extra='forbid' or 'allow') to change it. A red flag is claiming FastAPI 422s by default on unknown fields.

Python & FastAPI2 min read

Uvicorn Workers: Scaling Your FastAPI App

Uvicorn workers are like adding cashiers to a store. Instead of one process handling all requests, you run multiple, letting your FastAPI app use all CPU cores to serve more users concurrently.

What is the difference between a Pydantic default and Optional field?
Python & FastAPI2 min read

What is the difference between a Pydantic default and Optional field?

Both forms are non-required; str = 'guest' rejects None, Optional[str] = None accepts it.

FastAPI: Pydantic for Robust Request Bodies
Python & FastAPI2 min 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.

Enforce positive price and SKU format using Pydantic Field without custom validators
Python & FastAPI1 min read

Enforce positive price and SKU format using Pydantic Field without custom validators

Use Field(gt=0) for price and Field(pattern=r'^ITEM-\d{5}$') for SKU; mention Annotated.