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

Newest first

Explain Python type hints and their importance in FastAPI
Python & FastAPI2 min read

Explain Python type hints and their importance in FastAPI

Define hints as declarations; explain FastAPI uses them with Pydantic to validate requests and OpenAPI docs.

Python Type Hints: Documentation Your Linter Can Read
Python & FastAPI2 min 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.

What is the difference between def and async def in Python and FastAPI?
Python & FastAPI2 min read

What is the difference between def and async def in Python and FastAPI?

Tests event-loop boundaries: async def yields control via await for non-blocking I/O, def runs in a threadpool. Use async def only with async libraries; def covers blocking calls. Red flag: claiming async is automatically faster or awaiting inside def.

Python Data Classes: Write Less Boilerplate
Python & FastAPI2 min 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.

How does FastAPI leverage Pydantic for request validation and serialization?
Python & FastAPI2 min read

How does FastAPI leverage Pydantic for request validation and serialization?

This tests your understanding of FastAPI's declarative validation. Explain that type hints trigger auto-parsing, Pydantic enforces schemas and errors, and return types auto-serialize responses. Red flag: manually parsing request.body() or json.loads in routes.

Python Packages: Grouping Modules with __init__.py
Python & FastAPI2 min read

Python Packages: Grouping Modules with __init__.py

A Python package is a folder of modules treated as one unit. The __init__.py file marks the folder as a package and can run setup code. Use it to organize large codebases.

Python & FastAPI2 min read

Write an async decorator that logs execution time for FastAPI

Use functools.wraps, wrap perf_counter around awaited call, log ms, and place decorator above path operation.

Python Enums: Give Names to Magic Numbers
Python & FastAPI2 min 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.

How do you structure concurrent API calls with asyncio.gather in FastAPI?
Python & FastAPI2 min read

How do you structure concurrent API calls with asyncio.gather in FastAPI?

Tests FastAPI async concurrency. Strong answer: async def endpoint with two async HTTP requests in asyncio.gather, cutting total latency from sum to max of the two. Red flag: using sync clients or threads instead of async I/O.

Python Decorators: Functions that Wrap Functions
Python & FastAPI2 min read

Python Decorators: Functions that Wrap Functions

A decorator is a function that wraps another function, adding behavior without modifying the original code. They're used for caching, logging, or access control. The main footgun is forgetting that decorators run at definition time, not call time.

How does Uvicorn use asyncio to handle thousands of concurrent connections?
Python & FastAPI2 min read

How does Uvicorn use asyncio to handle thousands of concurrent connections?

Tests async concurrency and the GIL. Great answers cover the event loop suspending coroutines at await, Uvicorn interleaving connections, and multi-process workers for parallelism. Red flag: claiming asyncio uses threads per request or bypasses the GIL.

Python's `yield`: Functions That Pause and Resume
Python & FastAPI2 min read

Python's `yield`: Functions That Pause and Resume

Python's yield creates a generator: a pausable function that produces values on-demand, saving memory. Use it for large files or infinite sequences. The footgun: a generator is a one-time-use iterator; you can't loop over it twice.

Python & FastAPI2 min read

Implement an async database session dependency using yield for setup and teardown

This tests async resource lifecycle management in FastAPI. A strong answer uses async def, yields a session inside try, closes in finally, and injects with Depends. A red flag is omitting finally or using sync def for async I/O, which leaks connections.

The `with` Statement: Python's Automatic Cleanup Crew
Python & FastAPI2 min read

The `with` Statement: Python's Automatic Cleanup Crew

A context manager is Python's automatic cleanup crew. It uses the with statement to guarantee setup and teardown code runs, even if errors occur. It's essential for files and database connections.

Create a generic Pydantic BaseModel for API response wrappers
Python & FastAPI2 min read

Create a generic Pydantic BaseModel for API response wrappers

Subclass BaseModel and Generic[T]; type data as T; use ResponseWrapper[User]; note unparametrized TypeVars validate as Any.

Python Coroutines: Functions You Can Pause and Resume
Python & FastAPI2 min 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.

What is the purpose of @app.get("/") in FastAPI?
Python & FastAPI2 min read

What is the purpose of @app.get("/") in FastAPI?

Tests your understanding of FastAPI routing. A strong answer explains that the decorator binds an HTTP method and path to a Python function, registers it in the app's route table, and builds OpenAPI metadata.

Python's async/await: Concurrent, Not Parallel
Python & FastAPI2 min 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.

How do you define and access a FastAPI path parameter?
Python & FastAPI2 min read

How do you define and access a FastAPI path parameter?

Tests FastAPI route-to-function binding. Good answer: curly-brace syntax in the decorator path, a matching typed function argument, and awareness that FastAPI auto-extracts and converts the value.

Python & FastAPI1 min read

How FastAPI uses type hints for validation

Hints drive parsing, validation, and conversion; a path declared int is coerced or returns 422; OpenAPI is auto-generated.