Skip to main content

10 posts tagged with "fastapi"

fastapi tag description

View All Tags

Testing FastAPI Dependency Injection: A Comprehensive Guide

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Testing code that uses Dependency Injection (DI) is crucial because it allows you to isolate the logic of your route handlers from the complexity of external services (like databases, security checks, or configuration). FastAPI makes this straightforward using the app.dependency_overrides dictionary.

By implementing overrides, you replace the original, complex dependency function with a simple mock function that returns predictable test data, ensuring your tests run fast and consistently.

Advanced FastAPI Dependency Injection for Experts

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

For expert programmers, FastAPI Dependency Injection (DI) is not just about injecting configuration; it's a foundational tool for managing application state, enforcing transaction integrity, and implementing complex authorization logic cleanly and reliably. These advanced patterns leverage yield and nested dependencies to ensure stability and separation of concerns.

FastAPI Dependency Injection: Best Use Cases for Beginners

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

FastAPI's Dependency Injection (DI) system, powered by the Depends function, is a powerful concept built on simple Python functions. For beginners, the best use cases are those that demonstrate code reuse, logic separation, and early request validation without requiring complex external libraries.

By focusing on these three patterns, you learn to keep your route functions clean and focus only on the core business logic.

Singleton Pattern in FastAPI Dependency Injection

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. In web development, this is essential for managing resources like database pools, configuration objects, logging handlers, or complex machine learning models that are costly to initialize.

In FastAPI, while dependencies are generally executed once per request and the result is cached, this does not guarantee that the underlying class is instantiated only once across the entire application's lifecycle. To enforce a true global Singleton, we must use Python's built-in methods, typically overriding __new__.

FastAPI Dependency Injection: A Complete Guide to `Depends` and `yield`

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Dependency Injection (DI) is a core principle in FastAPI, allowing you to seamlessly integrate reusable logic, external resources (like database connections), configuration settings, and security checks into your API routes. FastAPI's DI system is built on standard Python features: type hinting and function calls, using the special Depends callable.

Benchmark: msgspec vs. Pydantic v2

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

msgspec is an extremely fast serialization and validation library that consistently outperforms Pydantic v2 in benchmarks. This performance advantage comes from its design as a lean, compiled-code-based library focused on a narrow set of data handling tasks, whereas Pydantic v2 is a feature-rich framework.

The performance differences are most pronounced in two key areas: parsing/decoding (converting data like JSON into Python objects) and serialization/encoding (converting Python objects into data like JSON).

Msgspec fastapi integration

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

msgspec is a Python library designed for high-performance serialization and validation, which makes it a powerful partner for FastAPI. It can be used as a drop-in replacement for FastAPI's default Pydantic models to handle data validation and schema generation, often leading to a significant speedup in API performance.

Measure execution time of a function or endpoint in FastAPI

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The best and most precise way to measure the execution time of a function or endpoint in FastAPI is by using a custom decorator or a middleware. This approach allows you to wrap your functions with timing logic without modifying the function's code itself, promoting clean, reusable, and maintainable code.

Supabase FastAPI integration

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Supabase Integration with FastAPI

Here's a guide to integrating Supabase with a FastAPI application. We'll cover environment setup, connecting to Supabase, and performing basic CRUD operations.